Calendar compareTo() Method in Java with Examples
The add(Calendar Calendar2) method of Calendar class is used to compare the time values or the millisecond offsets of this Calendar object with the passed Calendar object.
public int compareTo(Calendar Calendar2)
Parameters: The method takes one parameter Calendar2 of Calendar object type and refers to the object to be compared to this Calendar object.
Return Value: The method returns an integer value and can return any one of the following:
- The method returns 0 if the passed argument is equal to this Calendar object.
- The method returns 1 if the time of this Calendar object is more than the passed object.
- The method returns -1 if the time of this Calendar object is less than the passed object.
Below programs illustrate the working of compareTo() Method of Calendar class:
First comparison result is: 1 Second comparison result is: -1
The comparison result is: -1
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.