📜  Java中的 OffsetTime equals() 方法及示例

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

Java中的 OffsetTime equals() 方法及示例

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

方案二

// 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: Java/time/OffsetTime.html#compareTo-java.time.OffsetTime-