1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
/* Compare date time using after method of Java Calendar This example shows how to compare two calendar objects having different date and time using after method of Java Calendar class. */ import java.util.Calendar; public class CompareDateTimesUsingAfter { public static void main(String[] args) { //create first Calendar object Calendar futureCal = Calendar.getInstance(); //set it to some future date futureCal.set(Calendar.YEAR, 2010); //create second Calendar object Calendar now = Calendar.getInstance(); /* * To compare two different Calendar objects, use * boolean after(Caledar anotherCal) method. * * If the first Calendar object's date and time is after * anotherCal's date and time, * it returns true, false otherwise. */ System.out.println("Current date : " + (now.get(Calendar.MONTH) + 1) + "-" + now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR)); System.out.println("Is futureCal after now ? : " + futureCal.after(now)); } } /* Typical output would be Current date : 12-25-2007 Is futureCal after now ? : true */ |
Add Comment