可以使用isEqual()
Java中LocalDateTime类中的方法检查两个LocalDateTime对象是否相等。此方法需要一个参数,即要比较的LocalDateTime对象。如果两个LocalDateTime对象相等,则返回true,否则返回false。
演示此的程序如下所示-
import java.time.*; public class Main { public static void main(String[] args) { LocalDateTime ldt1 = LocalDateTime.parse("2019-02-18T23:15:30"); LocalDateTime ldt2 = LocalDateTime.parse("2019-02-18T23:15:30"); System.out.println("The LocalDateTime ldt1 is: " + ldt1); System.out.println("The LocalDateTime ldt2 is: " + ldt2); boolean flag = ldt1.isEqual(ldt2); if(flag) System.out.println("\nBoth LocalDateTime objects are equal"); else System.out.println("\nBoth LocalDateTime objects are not equal"); } }
输出结果
The LocalDateTime ldt1 is: 2019-02-18T23:15:30 The LocalDateTime ldt2 is: 2019-02-18T23:15:30 Both LocalDateTime objects are equal
现在让我们了解上面的程序。
显示两个LocalDateTime对象ldt1和ldt2。使用isEqual()
方法检查LocalDateTime对象是否相等。使用if语句显示返回的值。演示这的代码片段如下-
LocalDateTime ldt1 = LocalDateTime.parse("2019-02-18T23:15:30"); LocalDateTime ldt2 = LocalDateTime.parse("2019-02-18T23:15:30"); System.out.println("The LocalDateTime ldt1 is: " + ldt1); System.out.println("The LocalDateTime ldt2 is: " + ldt2); boolean flag = ldt1.isEqual(ldt2); if(flag) System.out.println("\nBoth LocalDateTime objects are equal"); else System.out.println("\nBoth LocalDateTime objects are not equal");