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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。