Java Instant compareTo()方法及示例

Java Instant compareTo()方法及示例

Instant类的 compareTo(Instant otherInstant) 方法用于比较本实例和作为参数传递的实例,以检查两个实例是否相等。两个实例之间的比较是基于两个实例的时间线位置。这个方法返回的值是按以下方式确定的。

  • 如果这个实例大于作为参数传递的实例,则返回一个正值
  • 如果这个实例等于作为参数传递的实例,则返回一个零(0)。
  • 如果该实例小于作为参数传递的实例,则返回一个负值。

语法

public int compareTo(Instant otherInstant)

参数: 该方法接受一个强制性的参数 otherInstant ,它是被比较的另一个瞬间,并且它不应该是空的。

返回值: 该方法返回一个整数值,如下所示。

  • 如果这个实例大于作为参数传递的实例,那么返回一个正值
  • 如果这个实例等于作为参数传递的实例,那么返回一个零(0)。
  • 如果这个实例小于作为参数传递的实例,则返回一个负值。

异常: 如果作为参数传递的其他Instant是空的,这个方法会抛出 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: 当作为参数传递的另一个Instant更大时

// 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)

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程