java.util.Date – Is a point in time (maintains millisecond accuracy)
java.util.Calendar –helps manipulate and compare java.util.Date
To convert Date to Calendar
Calendar cal1 = Calendar.getInstance();
Date currentTime = new Date(); // run on May, 28th 2009
cal1.setTime(currentTime);
assertEquals(2009, cal1.get(Calendar.YEAR)); // returns true
assertEquals(Calendar.MAY, cal1.get(Calendar.MONTH)); // returns true
assertEquals(28, cal1.get(Calendar.DATE)); // returns true
To convert Calendar to Date
Date date2 = cal1.getTime();
To find dates relative to each other – example to find the date tomorrow and yesterday
Calendar cal1 = Calendar.getInstance();
Date currentTime = new Date();// run on May, 28th 2009
cal1.setTime(currentTime);
cal1.roll(Calendar.DATE, 1); // one day after the date represented by cal1 i.e. 29th
assertEquals(2009, cal1.get(Calendar.YEAR)); // returns true
assertEquals(Calendar.MAY, cal1.get(Calendar.MONTH)); // returns true
assertEquals(29, cal1.get(Calendar.DATE)); // returns true
cal1.roll(Calendar.DATE, -1); // one day before the date represented by cal1 i.e. 28th
assertEquals(2009, cal1.get(Calendar.YEAR)); // returns true
assertEquals(Calendar.MAY, cal1.get(Calendar.MONTH)); // returns true
assertEquals(28, cal1.get(Calendar.DATE)); // returns true