Problem with Calendar Object in Leap Year
807591Feb 29 2008 — edited Feb 29 2008Hi All,
I have been using java calendar object for long but never saw issue which I face this year. Please see sample code below where I am assigning java calendar object with year, month, date, hour & minutes. The calendar object which gets created shows me in correct date.
public static void main(String[] argc){
Calendar calendar = Calendar.getInstance();
System.out.println( "***FINAL CALENDAR OBJECT ****** " + printCalendar(calendar));
calendar.set( Calendar.YEAR, 2008);
calendar.set( Calendar.MONTH, 2);
calendar.set( Calendar.DATE, 29);
calendar.set( Calendar.HOUR, 17);
calendar.set( Calendar.MINUTE, 35);
System.out.println( "***FINAL CALENDAR OBJECT ****** " + printCalendar(calendar));
}
private static String printCalendar(Calendar calendar){
SimpleDateFormat sdf = new SimpleDateFormat("d MMM yyyy hh:mm aaa");
String date = sdf.format(calendar.getTime());
return date;
}
Result which I am getting is given below
***FINAL CALENDAR OBJECT ****** 29 Feb 2008 10:59 AM
***FINAL CALENDAR OBJECT ****** 29 Mar 2008 05:35 PM
Please advice me what wrong I am doing or what needs to be done to get correct result.
Thanks in advance.