📜  Java 8 时钟 equals() 方法及示例

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

Java 8 时钟 equals() 方法及示例

Java Clock 类是Java的日期时间 API Java .time.Clock 的一部分。 Java日期时间 API 是从Java版本 8 添加的。
Java.time.Clock 类的 equals() 方法检查两个 Clock 对象是否相等。如果时钟相等则返回真,否则返回假。时钟类的 equals 方法覆盖此 Object.equals(Java.lang.Object) 方法以根据时钟对象状态进行比较。如果没有被覆盖,那么这个 equal() 方法采用Java.lang.Object.equals() 的描述

句法:

public boolean equals(Object obj)

参数:此方法需要一个强制参数obj ,它是传递的时钟对象,用于与现有时钟对象进行比较。
返回值:如果两个时钟对象相等,则此方法返回true 。否则,它返回false

例子:

Input: 
Clock object of ZoneId "UTC"
Clock object of ZoneId "Asia/calcutta"

Output:
false

Explanation:
Both objects represent Clock object of the different zone.
Hence applying equals on them returns false.

下面的程序说明了Java.time.Clock 类的 equals() 方法:

程序 1:当比较两个相似的类对象时。

Java
// Java program to demonstrate equals()
// method of Clock class
 
import java.time.Clock;
import java.time.ZoneId;
 
// create class
public class EqualsMethodDemo {
 
    // Main method
    public static void main(String[] args)
    {
 
        // create clock object which represents
        // UTC Zone time using system()
        Clock clock1 = Clock.system(ZoneId.of("Etc/UTC"));
 
        // Print Clock1 details
        System.out.println(clock1.toString());
 
        // Create another class Object using
        // clock class systemDefaultZone method
        Clock clock2 = Clock.systemDefaultZone();
 
        // Print Clock2 details
        System.out.println(clock2.toString());
 
        // check whether both clock objects are equal or not
        boolean equalResponse = clock1.equals(clock2);
 
        // print result
        System.out.println("Both clocks are equal:"
                           + equalResponse);
    }
}


Java
// Java program to demonstrate equals()
// method of Clock class
 
import java.time.Clock;
import java.time.ZoneId;
 
// create class
public class EqualsMethodDemo {
 
    // Main method
    public static void main(String[] args)
    {
 
        // Create a class Object using clock
        // class systemDefaultZone method
        Clock clock1 = Clock.systemDefaultZone();
 
        // Print Clock1 Zone details
        System.out.println("clock1 Time Zone = "
                           + clock1.getZone());
 
        // Create another class Object using
        // clock class systemUTC method
        Clock clock2 = Clock.systemUTC();
 
        // Print Clock2 Zone details
        System.out.println("clock2 Time Zone = "
                           + clock2.getZone());
 
        // check whether both clock objects are equal or not
        boolean equalResponse = clock1.equals(clock2);
 
        // print result
        System.out.println("Both clocks are equal:"
                           + equalResponse);
    }
}


输出:
SystemClock[Etc/UTC]
SystemClock[Etc/UTC]
Both clocks are equal:true

程序 2:当比较两个不同的类对象时。

Java

// Java program to demonstrate equals()
// method of Clock class
 
import java.time.Clock;
import java.time.ZoneId;
 
// create class
public class EqualsMethodDemo {
 
    // Main method
    public static void main(String[] args)
    {
 
        // Create a class Object using clock
        // class systemDefaultZone method
        Clock clock1 = Clock.systemDefaultZone();
 
        // Print Clock1 Zone details
        System.out.println("clock1 Time Zone = "
                           + clock1.getZone());
 
        // Create another class Object using
        // clock class systemUTC method
        Clock clock2 = Clock.systemUTC();
 
        // Print Clock2 Zone details
        System.out.println("clock2 Time Zone = "
                           + clock2.getZone());
 
        // check whether both clock objects are equal or not
        boolean equalResponse = clock1.equals(clock2);
 
        // print result
        System.out.println("Both clocks are equal:"
                           + equalResponse);
    }
}
输出:
clock1 Time Zone = Etc/UTC
clock2 Time Zone = Z
Both clocks are equal:false

参考: https: Java/time/Clock.html#equals-java.lang.Object-