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.
Syntax:
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:
Example 1:
// Java Code to illustrate compareTo() Method import java.util.*; public class CalendarClassDemo { public static void main(String args[]) { // Creating a calendar object Calendar calndr1 = Calendar.getInstance(); // Creating another calendar object Calendar calndr2 = new GregorianCalendar( 2018 , 12 , 2 ); // Comparing the time int val = calndr1.compareTo(calndr2); // Displaying the result of comparison System.out.println( "First" + " comparison result is: " + val); // Comparing the time val = calndr2.compareTo(calndr1); // Displaying the result of comparison System.out.println( "Second" + " comparison result is: " + val); } } |
First comparison result is: 1 Second comparison result is: -1
Example 2:
// Java Code to illustrate compareTo() Method import java.util.*; public class CalendarClassDemo { public static void main(String args[]) { // Creating a calendar object Calendar calndr1 = Calendar.getInstance(); // Creating another calendar object Calendar calndr2 = Calendar.getInstance(); // Comparing the time int val = calndr1.compareTo(calndr2); // Displaying the result of comparison System.out.println( "The" + " comparison result is: " + val); } } |
The comparison result is: -1
Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#compareTo(java.util.Calendar)
Recommended Posts:
- Double.compareTo() Method in Java with Examples
- ChronoZonedDateTime compareTo() method in Java with Examples
- ShortBuffer compareTo method in Java with Examples
- DoubleBuffer compareTo() method in Java With Examples
- Path compareTo() method in Java with Examples
- ByteBuffer compareTo() method in Java With Examples
- Year compareTo() method in Java with Examples
- LocalTime compareTo() method in Java with Examples
- Boolean compareTo() method in Java with examples
- Byte compareTo() method in Java with examples
- Instant compareTo() method in Java with Examples
- Short compareTo() method in Java with Examples
- MonthDay compareTo() method in Java with Examples
- Date compareTo() method in Java with examples
- ChronoLocalDate compareTo() method in Java with Examples
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.