Java中的MonthDay equals()方法
可以使用equals()Java中MonthDay类中的方法确定两个MonthDay对象的相等性。此方法需要单个参数,即要比较的MonthDay对象。如果两个MonthDay对象相等,则返回true,否则返回false。
演示此程序如下
示例
import java.time.*;
public class Main {
public static void main(String[] args) {
MonthDay md1 = MonthDay.parse("--02-22");
MonthDay md2 = MonthDay.parse("--02-22");
System.out.println("The MonthDay md1 is: " + md1);
System.out.println("The MonthDay md2 is: " + md2);
boolean flag = md1.equals(md2);
if(flag)
System.out.println("\nBoth MonthDay objects are equal");
else
System.out.println("\nBoth MonthDay objects are not equal");
}
}输出结果
The MonthDay md1 is: --02-22 The MonthDay md2 is: --02-22 Both MonthDay objects are equal
现在让我们了解上面的程序。
显示两个MonthDay对象。使用equals()方法检查MonthDay对象是否相等。使用if语句显示方法的返回值。演示此代码段如下所示:
MonthDay md1 = MonthDay.parse("--02-22");
MonthDay md2 = MonthDay.parse("--02-22");
System.out.println("The MonthDay md1 is: " + md1);
System.out.println("The MonthDay md2 is: " + md2);
boolean flag = md1.equals(md2);
if(flag)
System.out.println("\nBoth MonthDay objects are equal");
else
System.out.println("\nBoth MonthDay objects are not equal");