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


Java YearMonth equals()用法及代碼示例


Java中Yearyearth類的equals()方法用於比較兩個YearMonth對象。它將這個YearMonth對象與作為參數傳遞給它的YearMonth對象進行比較,並檢查兩個YearMonth實例是否相等。

用法

public boolean equals(Object otherYearMonth)

參數:此方法接受單個參數otherYearMonth,該參數是與此YearMonth進行比較的另一個YearMonth實例。


返回值:如果Yearyear等於otherYearMonth,則返回true,否則返回False。

以下程序說明了Java中YearMonth的equals()方法:
示例1:

// Program to illustrate the equals() method 
  
import java.util.*; 
import java.time.*; 
  
public class GfG { 
    public static void main(String[] args) 
    { 
  
        // Creates first YearMonth object 
        YearMonth firstYearMonth = YearMonth.of(2017, 8); 
  
        // Creates second YearMonth object 
        YearMonth secondYearMonth = YearMonth.of(2016, 11); 
  
        // check if the two YearMonth instances are equal 
        System.out.println(firstYearMonth.equals(secondYearMonth)); 
    } 
}
輸出:
false

示例2:

// Program to illustrate the equals() method 
  
import java.util.*; 
import java.time.*; 
  
public class GfG { 
    public static void main(String[] args) 
    { 
        // Creates first YearMonth object 
        YearMonth firstYearMonth = YearMonth.of(2017, 8); 
  
        // Creates second YearMonth object 
        YearMonth secondYearMonth = YearMonth.of(2017, 8); 
  
        // check if the two YearMonth instances are equal 
        System.out.println(firstYearMonth.equals(secondYearMonth)); 
    } 
}
輸出:
true

參考: https://docs.oracle.com/javase/8/docs/api/java/time/YearMonth.html#equals-java.lang.Object-



相關用法


注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 YearMonth equals() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。