Java 8 Clock equals()方法

可以使用equals()Java Clock Class中的方法检查两个Java时钟对象的相等性。该方法需要单个参数,即要与现有时钟对象进行比较的对象。如果两个时钟对象相等,则返回true,否则返回false。

演示此的程序如下所示-

示例

import java.time.Clock;
import java.time.ZoneId;
public class Demo {
   public static void main(String[] args) {
      Clock c1 = Clock.systemDefaultZone();
      Clock c2 = Clock.systemDefaultZone();
      System.out.println("Clock c1: " + c1.toString());
      System.out.println("Clock c2: " + c2.toString());
      boolean flag = c1.equals(c2);
      if(flag)
         System.out.println("\nBoth the clock objects are equal");
      else
         System.out.println("\nBoth the clock objects are not equal");
   }
}

输出结果

Clock c1: SystemClock[Etc/UTC]
Clock c2: SystemClock[Etc/UTC]
Both the clock objects are equal

现在让我们了解上面的程序。

使用该方法比较时钟对象c1和c2 equals(),并将返回值存储在标志中。然后显示对象是否相等。演示这的代码片段如下-

Clock c1 = Clock.systemDefaultZone();
Clock c2 = Clock.systemDefaultZone();
System.out.println("Clock c1: " + c1.toString());
System.out.println("Clock c2: " + c2.toString());
boolean flag = c1.equals(c2);
if(flag)
   System.out.println("\nBoth the clock objects are equal");
else
   System.out.println("\nBoth the clock objects are not equal");