Java offsetTime equals()方法及示例
Java中OffsetTime类的 equals() 方法检查这个时间是否等于另一个时间。如果它们相等则返回true,如果不相等则返回false。
语法
public boolean equals(Object obj)
参数: 该方法接受一个强制性参数 obj ,它指定了将被比较的其他时间。
返回值: 如果两个时间都相等,则返回真,否则返回假。
以下程序说明了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-