The compareTo() method of Year class in Java is used to compare this Year object with another Year object. The comparison of Year object is based on the values of Year.
public int compareTo(Year otherYear)
Parameter: This method accepts a single parameter otherYear. It is the Year object which specifies a year with which we want to compare the current year object.
Return Value: It returns an integeral comparator value based on the comparison of the two Year objects.
Below programs illustrate the compareTo() method of Year in Java:
Program 1: In this program the value of current Year object is less than the second year object. So, the value returned is -1.
Program 2: In this program the value of current Year object is equals to the second year object. So, the value returned is 0.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.