可以使用equals()
Java Instant类中的方法确定两个Instant对象的相等性。此方法需要一个参数,即要比较的Instant。如果两个Instant对象相等,则返回true,否则返回false。
演示此程序如下
import java.time.*; import java.time.temporal.ChronoUnit; public class Demo { public static void main(String[] args) { Instant i1 = Instant.parse("2019-01-13T16:10:35.00Z"); Instant i2 = Instant.parse("2019-01-13T16:10:35.00Z"); System.out.println("Instant object i1 is: " + i1); System.out.println("Instant object i2 is: " + i2); boolean flag = i1.equals(i2); if(flag) System.out.println("\nThe Instant objects are equal"); else System.out.println("\nThe Instant objects are not equal"); } }
输出结果
Instant object i1 is: 2019-01-13T16:10:35Z Instant object i2 is: 2019-01-13T16:10:35Z The Instant objects are equal
现在让我们了解上面的程序。
显示两个即时对象i1和i2。使用equals()
方法检查Instant对象是否相等。使用if语句显示方法的返回值。演示此代码段如下所示:
Instant i1 = Instant.parse("2019-01-13T16:10:35.00Z"); Instant i2 = Instant.parse("2019-01-13T16:10:35.00Z"); System.out.println("Instant object i1 is: " + i1); System.out.println("Instant object i2 is: " + i2); boolean flag = i1.equals(i2); if(flag) System.out.println("\nThe Instant objects are equal"); else System.out.println("\nThe Instant objects are not equal");