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
程序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-