Java中的 ChronoZonedDateTime isAfter() 方法及示例
Java中ChronoZonedDateTime 接口的isAfter()方法用于检查作为参数传递的日期是否在此 ChronoZonedDateTime 实例之后。它返回一个显示相同的布尔值。
句法:
public boolean isAfter(ChronoZonedDateTime otherDate)
参数:此方法接受参数otherDate ,该参数指定要与此 ChronoZonedDateTime 比较的其他日期时间。它不应该为空。
返回:该函数返回布尔值,显示此日期时间是否在指定的日期时间之后。
下面的程序说明了 ChronoZonedDateTime.isAfter() 方法:
方案一:
// 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
ChronoZonedDateTime dt1
= ZonedDateTime.parse(
"2018-12-06T19:21:12.123+05:30[Asia/Calcutta]");
// Prints the date
System.out.println(dt1);
// Parses the date
ChronoZonedDateTime dt2
= ZonedDateTime.parse(
"2018-10-06T19:21:12.123+05:30[Asia/Calcutta]");
// Prints the date
System.out.println(dt2);
// Compares both dates
System.out.println(dt1.isAfter(dt2));
}
}
输出:
2018-12-06T19:21:12.123+05:30[Asia/Calcutta]
2018-10-06T19:21:12.123+05:30[Asia/Calcutta]
true
方案二:
// 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
ChronoZonedDateTime dt1
= ZonedDateTime.parse(
"2018-10-06T19:21:12.123+05:30[Asia/Calcutta]");
// Prints the date
System.out.println(dt1);
// Parses the date
ChronoZonedDateTime dt2
= ZonedDateTime.parse(
"2018-12-06T19:21:12.123+05:30[Asia/Calcutta]");
// Prints the date
System.out.println(dt2);
// Compares both dates
System.out.println(dt1.isAfter(dt2));
}
}
输出:
2018-10-06T19:21:12.123+05:30[Asia/Calcutta]
2018-12-06T19:21:12.123+05:30[Asia/Calcutta]
false
参考: https://docs.oracle.com/javase/9/docs/api/ Java/time/chrono/ChronoZonedDateTime.html#isAfter-java.time.chrono.ChronoZonedDateTime-