Java ChronoLocalDateTime isBefore()方法及实例

来源:这里教程网 时间:2026-02-17 20:40:33 作者:

Java ChronoLocalDateTime isBefore()方法及实例

Java中ChronoLocalDateTime接口的 isBefore() 方法用于检查作为参数传递的日期是否在这个ChronoLocalDateTime实例之前。它返回一个显示相同的布尔值。

语法

default boolean isBefore(ChronoLocalDateTime otherDate)

参数。这个方法接受一个参数otherDate,它指定了要与这个ChronoLocalDateTime比较的其他日期时间。它不应该是空的。

返回: 该函数返回布尔值,显示该日期时间是否在指定日期时间之前。

以下程序说明了ChronoLocalDateTime.isBefore()方法。

程序1 :

// Program to illustrate the isBefore() 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.isBefore(dt2));    }}

输出:

2018-11-03T12:45:302016-12-04T12:45:30false

程序2

// Program to illustrate the isBefore() 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.isBefore(dt2));    }}

输出:

2018-11-03T12:45:302019-12-04T12:45:30true

参考资料: https://docs.oracle.com/javase/9/docs/api/java/time/chrono/ChronoLocalDateTime.html#isBefore-java.time.chrono.ChronoLocalDateTime-

相关推荐