YearMonth compareTo() method in Java
The compareTo() 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. The comparison between the two YearMonth instances is first done on the value of Year and then on the Month.
public int compareTo(YearMonth 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 an integral comparator value based on the comparison:
- It returns 1 is this YearMonth is greater than otherYearMonth.
- It returns -1 is this YearMonth is less than otherYearMonth.
- It returns 0 is this YearMonth is equals to otherYearMonth.
Below programs illustrate the compareTo() method of YearMonth in Java: