Java中的 LocalTime isBefore() 方法及示例
LocalTime类的isBefore()方法用于检查此 LocalTime 时间线位置是否在作为参数传递的 LocalTime 之前。如果此 LocalTime 时间线位置在作为参数传递的 LocalTime 之前,则该方法将返回 true,否则返回 false。比较基于时刻的时间线位置。
句法:
public boolean isBefore(LocalTime other)
参数:此方法接受一个参数 other,它是要比较的另一个 LocalTime对象。它不应该为空。
返回值:如果这个时间在指定时间之前,该方法返回true,否则返回false。
下面的程序说明了 isBefore() 方法:
方案一:
// Java program to demonstrate
// LocalTime.isBefore() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create a LocalTime object
LocalTime time1
= LocalTime.parse("19:34:50.63");
// create other LocalTime
LocalTime time2
= LocalTime.parse("23:14:00.63");
// print instances
System.out.println("LocalTime 1: " + time1);
System.out.println("LocalTime 2: " + time2);
// check if LocalTime is before LocalTime
// using isBefore()
boolean value = time1.isBefore(time2);
// print result
System.out.println("Is LocalTime1 before LocalTime2: "
+ value);
}
}
输出:
LocalTime 1: 19:34:50.630
LocalTime 2: 23:14:00.630
Is LocalTime1 before LocalTime2: true
方案二:
// Java program to demonstrate
// LocalTime.isBefore() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create a LocalTime object
LocalTime time1
= LocalTime.parse("23:59:11.98");
// create other LocalTime
LocalTime time2
= LocalTime.parse("10:24:53.21");
// print instances
System.out.println("LocalTime 1: " + time1);
System.out.println("LocalTime 2: " + time2);
// check if LocalTime is before LocalTime
// using isBefore()
boolean value = time1.isBefore(time2);
// print result
System.out.println("Is LocalTime1 before LocalTime2: "
+ value);
}
}
输出:
LocalTime 1: 23:59:11.980
LocalTime 2: 10:24:53.210
Is LocalTime1 before LocalTime2: false
参考: https: Java Java)