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-
相關用法
- Java Clock system()用法及代碼示例
- Java 8 Clock instant()用法及代碼示例
- Java Clock systemDefaultZone()用法及代碼示例
- Java 8 Clock offset()用法及代碼示例
- Java 8 Clock millis()用法及代碼示例
- Java 8 Clock fixed()用法及代碼示例
- Java 8 Clock getZone()用法及代碼示例
- Java Clock tick()用法及代碼示例
- Java 8 Clock hashCode()用法及代碼示例
- Java Clock tickSeconds()用法及代碼示例
- Java Clock systemUTC()用法及代碼示例
- Java Clock withZone()用法及代碼示例
- Java Clock tickMinutes()用法及代碼示例
- Java Set equals()用法及代碼示例
- Java Map equals()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Java 8 Clock equals() Method with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。