Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

After Method in Java Calendar To Compare Date and Time in Java

  1. import java.util.Calendar;
  2. public class AfterMethod {
  3. public static void main(String[] args) {
  4. //create first Calendar object
  5. Calendar futureCal = Calendar.getInstance();
  6. //set it to some future date
  7. futureCal.set(Calendar.YEAR, 2010);
  8. //create second Calendar object
  9. Calendar now = Calendar.getInstance();
  10. /*
  11. * To compare two different Calendar objects, use
  12. * boolean after(Caledar anotherCal) method.
  13. *
  14. * If the first Calendar object's date and time is after
  15. * anotherCal's date and time,
  16. * it returns true, false otherwise.
  17. */
  18. System.out.println("Current date : " + (now.get(Calendar.MONTH) + 1)
  19. + "-"
  20. + now.get(Calendar.DATE)
  21. + "-"
  22. + now.get(Calendar.YEAR));
  23. System.out.println("Is futureCal after now ? : " + futureCal.after(now));
  24. }
  25. }