Year compareTo() method in Java with Examples Last Updated : 20 Jul, 2021 Comments Improve Suggest changes Like Article Like Report 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.Syntax: 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 integral 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. Java // Program to illustrate the compareTo() method import java.util.*; import java.time.*; public class GfG { public static void main(String[] args) { // Creates first Year object Year firstYear = Year.of(2017); // Creates second year object Year secondYear = Year.of(2018); // Compare the two years and print the // comparator value System.out.println(firstYear.compareTo(secondYear)); } } Output: -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. Java // Program to illustrate the compareTo() method import java.util.*; import java.time.*; public class GfG { public static void main(String[] args) { // Creates first Year object Year firstYear = Year.of(2018); // Creates second year object Year secondYear = Year.of(2018); // Compare the two years and print the // comparator value System.out.println(firstYear.compareTo(secondYear)); } } Output: 0 Reference: https://docs.oracle.com/javase/8/docs/api/java/time/Year.html#compareTo-java.time.Year- Comment More infoAdvertise with us Next Article Float compareTo() method in Java with examples B barykrg Follow Improve Article Tags : Misc Java Java-Functions Java-time package Java-Year +1 More Practice Tags : JavaMisc Similar Reads Date compareTo() method in Java with examples The compareTo() method of Java Date class compares two dates and sort them for order. Syntax: public int compareTo(Date anotherDate) Parameters: The function accepts a single parameter anotherDate which specifies the date to be compared . Return Value: The function gives three return values specifie 2 min read Float compareTo() method in Java with examples The comapreTo() method of Float Class is a built-in method in Java that compares the two specified float values. The sign of the integer value returned is the same as that of the integer that would be returned by the function call. Syntax: public int compareTo(Object f) Parameters: The function acce 2 min read Float compareTo() method in Java with examples The comapreTo() method of Float Class is a built-in method in Java that compares the two specified float values. The sign of the integer value returned is the same as that of the integer that would be returned by the function call. Syntax: public int compareTo(Object f) Parameters: The function acce 2 min read Float compareTo() method in Java with examples The comapreTo() method of Float Class is a built-in method in Java that compares the two specified float values. The sign of the integer value returned is the same as that of the integer that would be returned by the function call. Syntax: public int compareTo(Object f) Parameters: The function acce 2 min read Short compareTo() method in Java with Examples The compareTo() method of Short class is a built in method in Java which is used to compare twoShort objects numerically. Syntax: public int compareTo(Short otherShort) Parameters : This method accepts a mandatory parameter otherShort which is the Short object to be compared. Return type : It return 2 min read Short compareTo() method in Java with Examples The compareTo() method of Short class is a built in method in Java which is used to compare twoShort objects numerically. Syntax: public int compareTo(Short otherShort) Parameters : This method accepts a mandatory parameter otherShort which is the Short object to be compared. Return type : It return 2 min read Like