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


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


Instant類的compareTo(Instant otherInstant)方法用於將該實例與作為參數傳遞的實例進行比較,以檢查兩個實例是否相等。兩種情況之間的比較是基於瞬間的時間線位置。該方法要返回的值確定如下:

  • 如果此實例大於作為參數傳遞的實例,則返回正值
  • 如果此實例等於作為參數傳遞的實例,則返回零(0)
  • 如果此實例小於作為參數傳遞的實例,則返回負值。

用法:

public int compareTo(Instant otherInstant)

參數:此方法接受強製參數otherInstant,該參數是要比較的另一個瞬間,並且不應為null。


返回值:此方法返回一個整數值,如下所示:

  • 如果此實例大於作為參數傳遞的實例,則返回正值
  • 如果此實例等於作為參數傳遞的實例,則返回零(0)
  • 如果此實例小於作為參數傳遞的實例,則返回負值。

異常:如果作為參數傳遞的otherInstant為null,則此方法將引發NullPointerException。

以下示例程序旨在說明Instant.compareTo()方法:

示例1:當此實例更大時

// Java program to demonstrate 
// Instant.compareTo() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create two instance objects 
        Instant instant1 
            = Instant.parse("2018-10-20T16:55:30.00Z"); 
  
        Instant instant2 
            = Instant.parse("2017-10-20T16:55:30.00Z"); 
  
        // print Instant Values 
        System.out.println("Instant1: "
                           + instant1); 
        System.out.println("Instant2: "
                           + instant2); 
  
        // compare both instant 
        int value = instant1.compareTo(instant2); 
  
        if (value > 0) 
            System.out.println("Instant1 is greater"); 
        else if (value == 0) 
            System.out.println("Instant1 is equal to Instant2"); 
        else
            System.out.println("Instant2 is greater"); 
    } 
}
輸出:
Instant1: 2018-10-20T16:55:30Z
Instant2: 2017-10-20T16:55:30Z
Instant1 is greater

示例2:當otherInstant作為參數傳遞時更大

// Java program to demonstrate 
// Instant.compareTo() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create two instance objects 
        Instant instant1 
            = Instant.parse("2017-10-20T16:55:30.00Z"); 
  
        Instant instant2 
            = Instant.parse("2018-10-20T16:55:30.00Z"); 
  
        // print Instant Values 
        System.out.println("Instant1: "
                           + instant1); 
        System.out.println("Instant2: "
                           + instant2); 
  
        // compare both instant 
        int value = instant1.compareTo(instant2); 
  
        if (value > 0) 
            System.out.println("Instant1 is greater"); 
        else if (value == 0) 
            System.out.println("Instant1 is equal to Instant2"); 
        else
            System.out.println("Instant2 is greater"); 
    } 
}
輸出:
Instant1: 2017-10-20T16:55:30Z
Instant2: 2018-10-20T16:55:30Z
Instant2 is greater

示例1:當兩個實例相等時

// Java program to demonstrate 
// Instant.compareTo() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create two instance objects 
        Instant instant1 
            = Instant.parse("2018-10-20T16:55:30.00Z"); 
  
        Instant instant2 
            = Instant.parse("2018-10-20T16:55:30.00Z"); 
  
        // print Instant Values 
        System.out.println("Instant1: "
                           + instant1); 
        System.out.println("Instant2: "
                           + instant2); 
  
        // compare both instant 
        int value = instant1.compareTo(instant2); 
  
        if (value > 0) 
            System.out.println("Instant1 is greater"); 
        else if (value == 0) 
            System.out.println("Instant1 is equal to Instant2"); 
        else
            System.out.println("Instant2 is greater"); 
    } 
}
輸出:
Instant1: 2018-10-20T16:55:30Z
Instant2: 2018-10-20T16:55:30Z
Instant1 is equal to Instant2

示例4:顯示NullPointer異常

// Java program to demonstrate 
// Instant.compareTo() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create two instance objects 
        Instant instant1 
            = Instant.parse("2018-10-20T16:55:30.00Z"); 
  
        Instant instant2 = null; 
  
        try { 
  
            // compare both instant 
            int value = instant1.compareTo(instant2); 
        } 
        catch (NullPointerException e) { 
            System.out.println("Exception: " + e); 
        } 
    } 
}
輸出:
Exception: java.lang.NullPointerException

參考文獻: https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#compareTo(java.time.Instant)



相關用法


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