使用isBefore()
Java中LocalDate类中的方法,可以检查特定的LocalDate是否在时间轴上早于另一个LocalDate 。此方法需要一个参数,即要比较的LocalDate对象。如果LocalDate对象在另一个LocalDate对象之前,则返回true,否则返回false。
演示此的程序如下所示-
import java.time.*; public class Main { public static void main(String[] args) { LocalDate ld1 = LocalDate.parse("2019-02-12"); LocalDate ld2 = LocalDate.parse("2019-02-14"); System.out.println("The LocalDate ld1 is: " + ld1); System.out.println("The LocalDate ld2 is: " + ld2); boolean flag = ld1.isBefore(ld2); if(flag) System.out.println("\nLocalDate object ld1 is before LocalDate object ld2"); else System.out.println("\nLocalDate object ld1 is after LocalDate object ld2"); } }
输出结果
The LocalDate ld1 is: 2019-02-12 The LocalDate ld2 is: 2019-02-14 LocalDate object ld1 is before LocalDate object ld2
现在让我们了解上面的程序。
显示两个LocalDate对象ld1和ld2。使用该isBefore()
方法检查时间线中的LocalDate对象ld1是否在LocalDate对象ld2之前。使用if语句显示返回的值。演示这的代码片段如下-
LocalDate ld1 = LocalDate.parse("2019-02-12"); LocalDate ld2 = LocalDate.parse("2019-02-14"); System.out.println("The LocalDate ld1 is: " + ld1); System.out.println("The LocalDate ld2 is: " + ld2); boolean flag = ld1.isBefore(ld2); if(flag) System.out.println("\nLocalDate object ld1 is before LocalDate object ld2"); else System.out.println("\nLocalDate object ld1 is after LocalDate object ld2");