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
相關用法
- Java OffsetTime isBefore()用法及代碼示例
- Java ChronoZonedDateTime isBefore()用法及代碼示例
- Java LocalDate isBefore()用法及代碼示例
- Java ChronoLocalDate isBefore()用法及代碼示例
- Java ChronoLocalDateTime isBefore()用法及代碼示例
- Java Year isBefore()用法及代碼示例
- Java MonthDay isBefore()用法及代碼示例
- Java LocalTime isBefore()用法及代碼示例
- Java Instant isBefore()用法及代碼示例
- Java YearMonth isBefore()用法及代碼示例
- Java OffsetDateTime isBefore()用法及代碼示例
- Java LocalDateTime with()用法及代碼示例
- Java LocalDateTime until()用法及代碼示例
- Java LocalDateTime now()用法及代碼示例
- Java LocalDateTime from()用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 LocalDateTime isBefore() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。