Compare two Calendar values

TypeMethodSummary
booleanafter(Object when)Returns whether this Calendar represents a time after the time represented by the specified Object.
booleanbefore(Object when)Returns whether this Calendar represents a time before the time represented by the specified Object.
intcompareTo(Calendar anotherCalendar)Compares the time values (millisecond offsets from the Epoch) represented by two Calendar objects.
booleanequals(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
java2s.com  |  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.