Java ChronoLocalDateTime isAfter()方法及实例
Java中ChronoLocalDateTime接口的 isAfter() 方法用于检查作为参数传递的日期是否在这个ChronoLocalDateTime实例之后。它返回一个显示相同的布尔值。
语法
default boolean isAfter(ChronoLocalDateTime otherDate)
参数。这个方法接受一个参数otherDate,它指定了要与这个ChronoLocalDateTime比较的其他日期时间。它不应该是空的。
返回: 该函数返回布尔值,显示该日期时间是否在指定日期时间之后。
以下程序说明了ChronoLocalDateTime.isAfter()方法。
程序1 :
// Program to illustrate the isAfter() method import java.util.*;import java.time.*;import java.time.chrono.*; public class GfG { public static void main(String[] args) { // Parses the date ChronoLocalDateTime dt1 = LocalDateTime.parse("2018-11-03T12:45:30"); // Prints the date System.out.println(dt1); // Parses the date ChronoLocalDateTime dt2 = LocalDateTime.parse("2016-12-04T12:45:30"); // Prints the date System.out.println(dt2); // Compares both dates System.out.println(dt1.isAfter(dt2)); }}
输出:
2018-11-03T12:45:302016-12-04T12:45:30true
程序2
// Program to illustrate the isAfter() method import java.util.*;import java.time.*;import java.time.chrono.*; public class GfG { public static void main(String[] args) { // Parses the date ChronoLocalDateTime dt1 = LocalDateTime.parse("2018-11-03T12:45:30"); // Prints the date System.out.println(dt1); // Parses the date ChronoLocalDateTime dt2 = LocalDateTime.parse("2019-12-04T12:45:30"); // Prints the date System.out.println(dt2); // Compares both dates System.out.println(dt1.isAfter(dt2)); }}输出:
2018-11-03T12:45:302019-12-04T12:45:30false
参考资料: https://docs.oracle.com/javase/9/docs/api/java/time/chrono/ChronoLocalDateTime.html#isAfter-java.time.chrono.ChronoLocalDateTime-
