Java中的LocalDateTime类的isBefore()方法检查此日期是否在指定的日期时间之前。
用法:
public boolean isAfter(ChronoLocalDateTime other)
参数:此方法接受参数other,这是要与之比较的其他日期时间。它不能为空。
返回值:该函数返回布尔值:如果此日期时间早于指定的日期时间。
以下示例程序旨在说明LocalDateTime.isBefore()方法:
示例1:
// 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));
}
}
输出:
Date 1: 2018-11-03T12:45:30
Date 2: 2016-12-04T12:45:30
Is Date 1 before Date 2: false
示例2:
// 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