可以使用isEqual()
Java中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-12"); System.out.println("The LocalDate ld1 is: " + ld1); System.out.println("The LocalDate ld2 is: " + ld2); boolean flag = ld1.isEqual(ld2); if(flag) System.out.println("\nBoth LocalDate objects are equal"); else System.out.println("\nBoth LocalDate objects are not equal"); } }
输出结果
The LocalDate ld1 is: 2019-02-12 The LocalDate ld2 is: 2019-02-12 Both LocalDate objects are equal
现在让我们了解上面的程序。
显示两个LocalDate对象ld1和ld2。使用isEqual()
方法检查LocalDate对象是否相等。使用if语句显示返回的值。演示这的代码片段如下-
LocalDate ld1 = LocalDate.parse("2019-02-12"); LocalDate ld2 = LocalDate.parse("2019-02-12"); System.out.println("The LocalDate ld1 is: " + ld1); System.out.println("The LocalDate ld2 is: " + ld2); boolean flag = ld1.isEqual(ld2); if(flag) System.out.println("\nBoth LocalDate objects are equal"); else System.out.println("\nBoth LocalDate objects are not equal");