Java 8 Clock equals() 方法
java 8programmingobject oriented programming
可以使用 Java Clock 类中的 equals() 方法检查两个 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("
Both the clock objects are equal"); else System.out.println("
Both the clock objects are not equal"); } }
输出
Clock c1: SystemClock[Etc/UTC] Clock c2: SystemClock[Etc/UTC] Both the clock objects are equal
现在让我们理解一下上面的程序。
使用方法 equals() 比较时钟对象 c1 和 c2,并将返回值存储在 flag 中。然后显示两个对象是否相等。以下代码片段演示了此过程:−
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("
Both the clock objects are equal"); else System.out.println("
Both the clock objects are not equal");