📜  Java中的即时 compareTo() 方法和示例

📅  最后修改于: 2022-05-13 01:54:58.796000             🧑  作者: Mango

Java中的即时 compareTo() 方法和示例

Instant 类compareTo(Instant otherInstant)方法用于将此实例与作为参数传递的实例进行比较,以检查两个实例是否相等。两个实例之间的比较基于时刻的时间线位置。该方法返回的值确定如下:

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

句法:

public int compareTo(Instant otherInstant)

参数:该方法接受一个强制参数otherInstant ,它是要比较的另一个时刻,它不应该为空。

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

  • 如果此实例大于作为参数传递的实例,则返回正值
  • 如果此实例等于作为参数传递的实例,则返回零 (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

方案二:当参数传递的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: Java Java.time.Instant)