Date and Calendar
Sunday, September 17th, 2006I also saw a recent post on the Java Development Network concerning U.S. Daylight Saving Time Changes in 2007.
Many Java developers will probably have had some problems using the Date and Calendar objects in any Java Developent Kit.
As a developer we are torn by the two objects as Date is what we want to use, but it’s Calendar that is entirely correct. Also: Calendar is quite big to serialize. If you want speed you would probably use Date or even the UTC value.
I personally (and my collegues as well) often mix dates and calendars. For calculations we use the calendars and dates we use for serialising and the front end. But still we fell into some traps.
The getTime method on the Calendar quite dangerous. The javadoc clearly specifies:
Returns a Date object representing this Calendar’s time value (millisecond offset from the Epoch”).
Still we use it, and what does it mean? Here is an example:
Calendar calendar1 = Calendar.getInstance(TimeZone.getTimeZone("CEST"));
Calendar calendar2 = Calendar.getInstance(TimeZone.getTimeZone("PST"));
Calendar calendar3 = Calendar.getInstance(TimeZone.getTimeZone("Asia/Kamchatka"));
System.out.println(calendar1.toString());
System.out.println(calendar1.getTime());
System.out.println(calendar2.toString());
System.out.println(calendar2.getTime());
System.out.println(calendar3.toString());
System.out.println(calendar3.getTime());
And here is the output of the code (my appalogies for the bad formatting):
java.util.GregorianCalendar[time=1158477463667,areFieldsSet=true,areAllFieldsSet =true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="GMT",offset=0,dstSavings=0,useD aylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ER A=1,YEAR=2006,MONTH=8,WEEK_OF_YEAR=38,WEEK_OF_MONTH=4,DAY_OF_MONTH=17, DAY_OF_YEAR=260,DAY_OF_WEEK=1,DAY_OF_WEEK_IN_MONTH=3,AM_PM=0,HOUR=7,HO UR_OF_DAY=7,MINUTE=17,SECOND=43,MILLISECOND=667,ZONE_OFFSET=0,DST_OFFSET =0] Sun Sep 17 09:17:43 CEST 2006 java.util.GregorianCalendar[time=1158477463667,areFieldsSet=true,areAllFieldsSet=true, lenient=true,zone=sun.util.calendar.ZoneInfo[id="PST",offset=-28800000,dstSavings=360 0000,useDaylight=true,transitions=185,lastRule=java.util.SimpleTimeZone[id=PST,offset= -28800000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth= 2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,end Month=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]],firstDayOf Week=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2006,MONTH=8,WEEK_OF_YEAR=38,W EEK_OF_MONTH=4,DAY_OF_MONTH=17,DAY_OF_YEAR=260,DAY_OF_WEEK=1,DAY_OF_W EEK_IN_MONTH=3,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=17,SECOND=43,MILLIS ECOND=667,ZONE_OFFSET=-28800000,DST_OFFSET=3600000] Sun Sep 17 09:17:43 CEST 2006 java.util.GregorianCalendar[time=1158477463696,areFieldsSet=true,areAllFieldsSet=true ,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Kamchatka",offset=43200000,dst Savings=3600000,useDaylight=true,transitions=118,lastRule=java.util.SimpleTimeZone[i d=Asia/Kamchatka,offset=43200000,dstSavings=3600000,useDaylight=true,startYear=0 ,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=7200000,startTi meMode=1,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=7200000, endTimeMode=1]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2006,MO NTH=8,WEEK_OF_YEAR=38,WEEK_OF_MONTH=4,DAY_OF_MONTH=17,DAY_OF_YEAR=26 0,DAY_OF_WEEK=1,DAY_OF_WEEK_IN_MONTH=3,AM_PM=1,HOUR=8,HOUR_OF_DAY=20 ,MINUTE=17,SECOND=43,MILLISECOND=696,ZONE_OFFSET=43200000,DST_OFFSET=3 600000] Sun Sep 17 09:17:43 CEST 2006
As you see: the Date object is created with the UTC code and does not know anything about timezones, the dates are set the the local timezone (CEST in my case).
If you what to be on the save side: allways use Calendar. Or consider using Joda-Time.