Compare two Calendar values

boolean after(Object when)
Returns whether this Calendar represents a time after the time represented by the specified Object.
boolean before(Object when)
Returns whether this Calendar represents a time before the time represented by the specified Object.
int compareTo(Calendar anotherCalendar)
Compares the time values (millisecond offsets from the Epoch) represented by two Calendar objects.
boolean equals(Object obj)
Compares this Calendar to the specified Object.

Compare date time using after method


import java.util.Calendar;

public class Main {
  public static void main(String[] args) {

    Calendar future = Calendar.getInstance();
    future.set(Calendar.YEAR, 3010);
    
    Calendar now = Calendar.getInstance();
    
    System.out.println("Is futureCal after now ? : " + future.after(now));
  }
}

The output:


Is futureCal after now ? : true

Compare date time using before method


import java.util.Calendar;

public class Main {
  public static void main(String[] args) {
    Calendar old = Calendar.getInstance();
    old.set(Calendar.YEAR, 1990);
 
    Calendar now = Calendar.getInstance();
    
    System.out.println("Is old before now ? : " + old.before(now));
  }
}

The output:


Is old before now ? : true
Home 
  Java Book 
    Essential Classes  

Calendar:
  1. Calendar class
  2. Constants value in Calendar
  3. Create new Calendar instance
  4. Get field value from Calendar
  5. Compare two Calendar values
  6. Calendar and display name
  7. Is lenient
  8. Add/set value to a field and get new calendar value
  9. Convert Calendar value to string value