當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。