Instant類的isAfter()方法用於檢查此瞬間是否在作為參數傳遞的瞬間之後。此方法返回一個顯示相同值的布爾值。
用法:
public boolean isAfter(Instant otherInstant)
參數:此方法采用參數otherInstant,該參數是要與該瞬間進行比較的另一瞬間。它不能為空。
返回值:如果此時刻在指定時刻之後,則此方法返回true。否則返回false。
異常:如果以參數為null傳遞的otherInstant,則此方法將引發NullPointerException。
以下示例程序旨在說明isAfter()方法:
示例1:
// Java program to demonstrate
// Instant.isAfter() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create a Instant object
Instant instant1
= Instant.parse("2018-12-30T19:34:50.63Z");
// create other Instant
Instant instant2
= Instant.parse("2018-12-29T09:24:00.63Z");
// print instances
System.out.println("Instance 1: " + instant1);
System.out.println("Instance 2: " + instant2);
// check if instant1 is after instant2
// using isAfter()
boolean value = instant1.isAfter(instant2);
// print result
System.out.println("Is Instant1 after Instant2: "
+ value);
}
}
輸出:
Instance 1: 2018-12-30T19:34:50.630Z Instance 2: 2018-12-29T09:24:00.630Z Is Instant1 after Instant2: true
示例2:
// Java program to demonstrate
// Instant.isAfter() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create a Instant object
Instant instant1
= Instant.parse("2018-10-30T19:34:50.63Z");
// create other Instant
Instant instant2 = Instant.now();
// print instances
System.out.println("Instance 1: " + instant1);
System.out.println("Instance 2: " + instant2);
// check if instant1 is after instant2
// using isAfter()
boolean value = instant1.isAfter(instant2);
// print result
System.out.println("Is Instant1 after Instant2: "
+ value);
}
}
輸出:
Instance 1: 2018-10-30T19:34:50.630Z Instance 2: 2018-11-27T04:52:08.970Z Is Instant1 after Instant2: false
示例3:顯示isAfter()引發的異常
// Java program to demonstrate
// Instant.isAfter() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create a Instant object
Instant instant1
= Instant.parse("2018-10-30T19:34:50.63Z");
// create other Instant
Instant instant2 = null;
try {
// print instances
System.out.println("Instance 1: " + instant1);
System.out.println("Instance 2: " + instant2);
// check if instant1 is after instant2
// using isAfter()
boolean value = instant1.isAfter(instant2);
}
catch (Exception e) {
// print result
System.out.println("Exception: " + e);
}
}
}
輸出:
Instance 1: 2018-10-30T19:34:50.630Z Instance 2: null Exception: java.lang.NullPointerException
參考文獻: https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#isAfter(java.time.Instant)
相關用法
- Java LocalDateTime isAfter()用法及代碼示例
- Java ChronoZonedDateTime isAfter()用法及代碼示例
- Java OffsetDateTime isAfter()用法及代碼示例
- Java Year isAfter()用法及代碼示例
- Java LocalTime isAfter()用法及代碼示例
- Java OffsetTime isAfter()用法及代碼示例
- Java MonthDay isAfter()用法及代碼示例
- Java ChronoLocalDateTime isAfter()用法及代碼示例
- Java LocalDate isAfter()用法及代碼示例
- Java YearMonth isAfter()用法及代碼示例
- Java ChronoLocalDate isAfter()用法及代碼示例
- Java Instant with()用法及代碼示例
- Java Instant plus()用法及代碼示例
- Java Instant from()用法及代碼示例
- Java Instant now()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Instant isAfter() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。