Java中的OffsetTime類的equals()方法檢查該時間是否等於另一個時間。如果相等則返回true,否則返回false。
用法:
public boolean equals(Object obj)
參數:此方法接受單個強製性參數obj,該參數指定將與之進行比較的其他時間。
返回值:如果兩個時間相等,則返回true,否則返回false。
以下示例程序旨在說明equals()方法:
示例1:
// Java program to demonstrate the equals() method
import java.time.OffsetTime;
public class GFG {
public static void main(String[] args)
{
// Parses the time
OffsetTime time1
= OffsetTime.parse("15:30:30+07:00");
// Parses the time
OffsetTime time2
= OffsetTime.parse("15:30:30+07:00");
// gets the offset time1
System.out.println("time1: "
+ time1);
// gets the offset time2
System.out.println("time1: "
+ time2);
System.out.println("time1 when compared"
+ " to time2 returns: "
+ time1.equals(time2));
}
}
輸出:
time1: 15:30:30+07:00 time1: 15:30:30+07:00 time1 when compared to time2 returns: true
程序2:
// Java program to demonstrate the equals() method
import java.time.OffsetTime;
public class GFG {
public static void main(String[] args)
{
// Parses the time
OffsetTime time1
= OffsetTime.parse("15:30:30+07:00");
// Parses the time
OffsetTime time2
= OffsetTime.parse("12:10:30+07:00");
// gets the offset time1
System.out.println("time1: "
+ time1);
// gets the offset time2
System.out.println("time1: "
+ time2);
System.out.println("time1 when compared"
+ " to time2 returns: "
+ time1.equals(time2));
}
}
輸出:
time1: 15:30:30+07:00 time1: 12:10:30+07:00 time1 when compared to time2 returns: false
參考: https://docs.oracle.com/javase/8/docs/api/java/time/OffsetTime.html#compareTo-java.time.OffsetTime-
相關用法
- Java OffsetTime plus()用法及代碼示例
- Java OffsetTime until()用法及代碼示例
- Java OffsetTime with()用法及代碼示例
- Java OffsetTime now()用法及代碼示例
- Java OffsetTime get()用法及代碼示例
- Java OffsetTime from()用法及代碼示例
- Java OffsetTime minusSeconds()用法及代碼示例
- Java OffsetTime minusMinutes()用法及代碼示例
- Java OffsetTime query()用法及代碼示例
- Java OffsetTime toString()用法及代碼示例
- Java OffsetTime plusMinutes()用法及代碼示例
- Java OffsetTime range()用法及代碼示例
- Java OffsetTime toLocalTime()用法及代碼示例
- Java OffsetTime minusHours()用法及代碼示例
- Java OffsetTime minusNanos()用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 OffsetTime equals() method in Java with examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。