
- Java.util Package Classes
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util Package Extras
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java.util.Calendar.compareTo() Method
Description
The java.util.Calendar.compareTo() method compares the time values (millisecond offsets) between the Calendar object and anotherCalendar object.
Declaration
Following is the declaration for java.util.Calendar.compareTo() method
public int compareTo(Calendar anotherCalendar)
Parameters
anotherCalendar − the Calendar object to be compared.
Return Value
The method returns 0 if the time represented by the argument is equal to the time represented by this Calendar object; or a value less than 0 if the time of this Calendar is before the time represented by the argument; or a value greater than 0 if the time of this Calendar is after the time represented.
Exception
NullPointerException − if the specified Calendar is null.
IllegalArgumentException − if the time value of the specified Calendar object can't be obtained
Example
The following example shows the usage of java.util.calendar.compareTo() method.
package com.tutorialspoint; import java.util.*; public class CalendarDemo { public static void main(String[] args) { // create two calendar at the different dates Calendar cal1 = new GregorianCalendar(2015, 8, 15); Calendar cal2 = new GregorianCalendar(2008, 1, 02); // compare the time values represented by two calendar objects. int i = cal1.compareTo(cal2); // return positive value if equals else return negative value System.out.println("The result is :"+i); // compare again but with the two calendars swapped int j = cal2.compareTo(cal1); // return positive value if equals else return negative value System.out.println("The result is :" + j); } }
Let us compile and run the above program, this will produce the following result −
The result is :1 The result is :-1