📜  Java中的 YearMonth equals() 方法

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

Java中的 YearMonth equals() 方法

Java中 YearMonth 类的 equals() 方法用于比较两个 YearMonth 对象。它将这个 YearMonth 对象与作为参数传递给它的 YearMonth 对象进行比较,并检查两个 YearMonth 实例是否相等。

语法

public boolean equals(Object otherYearMonth)

参数:此方法接受单个参数otherYearMonth ,它是要与此 YearMonth 进行比较的另一个 YearMonth 实例。

返回值:如果这个 YearMonth 等于 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

方案二

// 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: Java/time/YearMonth.html#equals-java.lang.Object-