📌  相关文章
📜  Java中的 LocalDateTime isBefore() 方法及示例

📅  最后修改于: 2022-05-13 01:54:49.069000             🧑  作者: Mango

Java中的 LocalDateTime isBefore() 方法及示例

Java中LocalDateTime 类isBefore()方法检查此日期是否早于指定的日期时间。

句法:

public boolean isBefore(ChronoLocalDateTime other)

参数:此方法接受参数other是要比较的另一个日期时间。它不应该为空。

返回:函数返回布尔值:如果此日期时间早于指定的日期时间。

下面的程序说明了 LocalDateTime.isBefore() 方法:

方案一:

Java
// Program to illustrate the isBefore() method
 
import java.util.*;
import java.time.*;
 
public class GfG {
    public static void main(String[] args)
    {
        // Parses the date
        LocalDateTime dt1
            = LocalDateTime
                  .parse("2018-11-03T12:45:30");
 
        // Prints the date
        System.out.println("Date 1: " + dt1);
 
        // Parses the date
        LocalDateTime dt2
            = LocalDateTime
                  .parse("2016-12-04T12:45:30");
 
        // Prints the date
        System.out.println("Date 2: " + dt2);
 
        // Compares both dates
        System.out.println("Is Date 1 before Date 2: "
                           + dt1.isBefore(dt2));
    }
}


Java
// Program to illustrate the isBefore() method
 
import java.util.*;
import java.time.*;
 
public class GfG {
    public static void main(String[] args)
    {
        // Parses the date
        LocalDateTime dt1
            = LocalDateTime
                  .parse("2018-11-03T12:45:30");
 
        // Prints the date
        System.out.println("Date 1: " + dt1);
 
        // Parses the date
        LocalDateTime dt2
            = LocalDateTime
                  .parse("2019-12-04T12:45:30");
 
        // Prints the date
        System.out.println("Date 2: " + dt2);
 
        // Compares both dates
        System.out.println("Is Date 1 before Date 2: "
                           + dt1.isBefore(dt2));
    }
}


输出:
Date 1: 2018-11-03T12:45:30
Date 2: 2016-12-04T12:45:30
Is Date 1 before Date 2: false

方案二:

Java

// Program to illustrate the isBefore() method
 
import java.util.*;
import java.time.*;
 
public class GfG {
    public static void main(String[] args)
    {
        // Parses the date
        LocalDateTime dt1
            = LocalDateTime
                  .parse("2018-11-03T12:45:30");
 
        // Prints the date
        System.out.println("Date 1: " + dt1);
 
        // Parses the date
        LocalDateTime dt2
            = LocalDateTime
                  .parse("2019-12-04T12:45:30");
 
        // Prints the date
        System.out.println("Date 2: " + dt2);
 
        // Compares both dates
        System.out.println("Is Date 1 before Date 2: "
                           + dt1.isBefore(dt2));
    }
}
输出:
Date 1: 2018-11-03T12:45:30
Date 2: 2019-12-04T12:45:30
Is Date 1 before Date 2: true

参考: https: Java Java.time.chrono.ChronoLocalDateTime)