YearMonth equals() method in Java

The equals() method of YearMonth class in Java is used to compare two YearMonth objects. It compares this YearMonth object to the YearMonth object passed to it as parameter and checks if two YearMonth instances are equal or not.

Syntax:

public boolean equals(Object otherYearMonth)

Parameter: This method accepts a single parameter otherYearMonth which is the other YearMonth instance with which this YearMonth is to be compared.

Return Value: It returns true if this YearMonth is equal to otherYearMonth otherwise it returns False.

Below programs illustrate the equals() method of YearMonth in Java:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


Output:

false

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


Output:

true

Reference: https://docs.oracle.com/javase/8/docs/api/java/time/YearMonth.html#equals-java.lang.Object-



My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.