📌  相关文章
📜  Java中的 ZoneOffset equal(Object) 方法及示例

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

Java中的 ZoneOffset equal(Object) 方法及示例

Java.time 包ZoneOffset 类equals(Object)方法用于检查作为参数传递给此ZoneOffset 实例的另一个ZoneOffset 实例是否相等。此方法返回一个布尔值,说明相同。

句法:

public boolean equals(Object object)

参数:此方法接受一个参数对象,该对象将等于此 ZoneOffset 实例。

返回值:此方法返回一个布尔值,说明此 ZoneOffset 是否等于作为参数传递的实例。

下面的示例说明了 ZoneOffset.equals() 方法:

示例 1:

// Java code to illustrate equals() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Get the ZoneOffset instance
        ZoneOffset zoneOffset1
            = ZoneOffset.ofHours(5);
        System.out.println("ZoneOffset 1: "
                           + zoneOffset1);
  
        // Get the ZoneOffset instance
        ZoneOffset zoneOffset2
            = ZoneOffset.ofHours(5);
        System.out.println("ZoneOffset 2: "
                           + zoneOffset2);
  
        // Using equals() method
        System.out.println("ZoneOffset 1 "
                           + "is equal to ZoneOffset 2: "
                           + zoneOffset1.equals(zoneOffset2));
    }
}
输出:
ZoneOffset 1: +05:00
ZoneOffset 2: +05:00
ZoneOffset 1 is equal to ZoneOffset 2: true

示例 2:

// Java code to illustrate equals() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Get the ZoneOffset instance
        ZoneOffset zoneOffset1
            = ZoneOffset.ofHours(5);
        System.out.println("ZoneOffset 1: "
                           + zoneOffset1);
  
        // Get the ZoneOffset instance
        ZoneOffset zoneOffset3
            = ZoneOffset.ofHours(3);
        System.out.println("ZoneOffset 3: "
                           + zoneOffset3);
  
        // Using equals() method
        System.out.println("ZoneOffset 1 "
                           + "is equal to ZoneOffset 3: "
                           + zoneOffset1.equals(zoneOffset3));
    }
}
输出:
ZoneOffset 1: +05:00
ZoneOffset 3: +03:00
ZoneOffset 1 is equal to ZoneOffset 3: false

参考: Oracle 文档