可以使用Java中LocalDateTime类中的isAfter()方法来检查特定LocalDateTime是否在时间轴上位于另一个LocalDateTime之后。此方法需要一个参数,即要比较的LocalDateTime对象。如果LocalDateTime对象在另一个LocalDateTime对象之后,则返回true,否则返回false。
演示此的程序如下所示-
import java.time.*; public class Main { public static void main(String[] args) { LocalDateTime ldt1 = LocalDateTime.parse("2019-02-20T11:37:12"); 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.isAfter(ldt2); if(flag) System.out.println("\nLocalDateTime object ldt1 is after LocalDateTime object ldt2"); else System.out.println("\nLocalDateTime object ldt1 is before LocalDateTime object ldt2"); } }
输出结果
The LocalDateTime ldt1 is: 2019-02-20T11:37:12 The LocalDateTime ldt2 is: 2019-02-18T23:15:30 LocalDateTime object ldt1 is after LocalDateTime object ldt2
现在让我们了解上面的程序。
显示两个LocalDateTime对象ldt1和ldt2。使用isAfter()方法检查时间线中LocalDateTime对象ldt1是否在LocalDateTime对象ldt2之后。使用if语句显示返回的值。演示这的代码片段如下-
LocalDateTime ldt1 = LocalDateTime.parse("2019-02-20T11:37:12"); 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.isAfter(ldt2); if(flag) System.out.println("\nLocalDateTime object ldt1 is after LocalDateTime object ldt2"); else System.out.println("\nLocalDateTime object ldt1 is before LocalDateTime object ldt2");