当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java 8 Clock equals()用法及代码示例


Java Clock类是Java的Date Time API(java.time.Clock)的一部分。 Java日期时间API是从Java版本8添加的。

java.time.Clock类的equals()方法检查两个Clock对象是否相等。如果时钟相等,则返回true,否则返回false。时钟类的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 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 obejcts 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 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 obejcts 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://docs.oracle.com/javase/8/docs/api/java/time/Clock.html#equals-java.lang.Object-



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 Java 8 Clock equals() Method with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。