當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Instant isBefore()用法及代碼示例


Instant類的isBefore()方法檢查此即時時間軸位置是否在作為參數傳遞的即時數據之前。如果此瞬時時間軸位置在作為參數傳遞的瞬時之前,則該方法將返回true或false。比較是基於瞬時的時間線位置。

用法:

public boolean isBefore(Instant otherInstant)

參數:此方法采用參數otherInstant,該參數是要與之進行比較的另一個瞬間。它不能為空。


返回值:如果此時刻在指定時刻之前,則此方法返回true。

異常:如果otherInstant為null,則此方法引發NullPointerException。

以下示例程序旨在說明isBefore()方法:

示例1:

// Java program to demonstrate 
// Instant.isBefore() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a Instant object 
        Instant instant1 
            = Instant.parse("2018-12-30T09:24:54.63Z"); 
  
        // create other Instant 
        Instant instant2 
            = Instant.parse("2018-12-31T01:34: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.isBefore(instant2); 
  
        // print result 
        System.out.println("Is Instant1 before Instant2: "
                           + value); 
    } 
}
輸出:
Instance 1: 2018-12-30T09:24:54.630Z
Instance 2: 2018-12-31T01:34:00.630Z
Is Instant1 before Instant2: true

示例2:

// Java program to demonstrate 
// Instant.isBefore() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a Instant object 
        Instant instant1 
            = Instant.parse("2018-11-27T09:24:54.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.isBefore(instant2); 
  
        // print result 
        System.out.println("Is Instant1 before Instant2: "
                           + value); 
    } 
}
輸出:
Instance 1: 2018-11-27T09:24:54.630Z
Instance 2: 2018-11-27T04:55:36.127Z
Is Instant1 before Instant2: false

示例3:顯示isBefore()引發的異常

// Java program to demonstrate 
// Instant.isBefore() 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.isBefore(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#isBefore(java.time.Instant)



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Instant isBefore() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。