Java MonthDay equals()方法及示例

来源:这里教程网 时间:2026-02-17 20:58:58 作者:

Java MonthDay equals()方法及示例

Java中MonthDay类的 equals() 方法可以检查这个月日是否与另一个月日相等。

语法

public boolean equals(Object obj)

参数。该方法接受一个参数obj,指定这个月日是否等于另一个月日。

返回: 如果这个时间等于另一个时间,该函数返回真。

下面的程序说明了 MonthDay.equals() 方法。

程序1 :

// Program to illustrate the equals() method  import java.util.*;import java.time.*;  public class GfG {    public static void main(String[] args)    {        // Parses the date        MonthDay tm1 = MonthDay.parse("--12-06");          // Uses the function        LocalDate dt1 = tm1.atYear(2018);          // Parses the date        MonthDay tm2 = MonthDay.parse("--12-06");          // Uses the function        LocalDate dt2 = tm2.atYear(2018);          // Prints the date        System.out.println(dt1.equals(dt2));    }}

输出。

true

程序2

// Program to illustrate the equals() method  import java.util.*;import java.time.*;  public class GfG {    public static void main(String[] args)    {        // Parses the date        MonthDay tm1 = MonthDay.parse("--10-06");          // Uses the function        LocalDate dt1 = tm1.atYear(2018);          // Parses the date        MonthDay tm2 = MonthDay.parse("--12-06");          // Uses the function        LocalDate dt2 = tm2.atYear(2018);          // Prints the date        System.out.println(dt1.equals(dt2));    }}

输出。

false

参考资料: https://docs.oracle.com/javase/8/docs/api/java/time/MonthDay.html#equals-java.lang.Object-

相关推荐