当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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 Doc



相关用法


注:本文由纯净天空筛选整理自Code_r大神的英文原创作品 ZoneOffset equal(Object) method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。