当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。