JDK-6609456 : Calendar object returned by DateFormat is not always initialised correctly.
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util:i18n
  • Affected Version: 1.4.2
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2007-09-26
  • Updated: 2010-07-29
  • Resolved: 2007-10-19
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.5.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64)
Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows 2000 [Version 5.00.2195]
(SP4)

A DESCRIPTION OF THE PROBLEM :
Getting a Calendar instance using the following:
DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.MEDIUM).getCalendar();

Results in a Calendar instance that has an incorrect YEAR and DST_OFFSET (retrieved using Calendar.get). The YEAR is set to 1924 in the case I ran and the DST_OFFSET stays at 0 regardless of the DST status of the OS.

Instances returned by Calendar.getInstance() do not appear to have this problem.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the attached code, with the timezone on Windows set to not "Automatically adjust for DST" and then again with it set. The output shown below shows the incorrect fields for DateFormat.getCalendar().

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
I would expect that the instance returned by DateFormat.getCalendar() and Calendar.getInstance() to be equivalent.
ACTUAL -
With DST off the following is produced:

<pre>
Using Calendar.getInstance():
Zone Offset:  -18000000
DST Offset:   0
Year:         2004
Current Time: Sun Oct 24 23:10:50 GMT-05:00 2004
java.util.GregorianCalendar[time=1098677450585,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="GMT-05:00",offset=-18000000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2004,MONTH=9,WEEK_OF_YEAR=44,WEEK_OF_MONTH=5,DAY_OF_MONTH=24,DAY_OF_YEAR=298,DAY_OF_WEEK=1,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=11,HOUR_OF_DAY=23,MINUTE=10,SECOND=50,MILLISECOND=585,ZONE_OFFSET=-18000000,DST_OFFSET=0]

Using DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.MEDIUM).getCalendar():
Zone Offset:  -18000000
DST Offset:   0
Year:         1924
Current Time: Fri Oct 24 23:10:50 GMT-05:00 1924
java.util.GregorianCalendar[time=-1425930549345,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="GMT-05:00",offset=-18000000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=1924,MONTH=9,WEEK_OF_YEAR=43,WEEK_OF_MONTH=4,DAY_OF_MONTH=24,DAY_OF_YEAR=298,DAY_OF_WEEK=6,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=11,HOUR_OF_DAY=23,MINUTE=10,SECOND=50,MILLISECOND=655,ZONE_OFFSET=-18000000,DST_OFFSET=0]
</pre>

Output from Calendar.getInstance() is correct whereas the DateFormat.getCalendar() has the year incorrect. DST offset is correctly 0 in both cases.


Now with DST on:

<pre>
Using Calendar.getInstance():
Zone Offset:  -18000000
DST Offset:   3600000
Year:         2004
Current Time: Mon Oct 25 00:14:02 EDT 2004
java.util.GregorianCalendar[time=1098677642474,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="America/New_York",offset=-18000000,dstSavings=3600000,useDaylight=true,transitions=235,lastRule=java.util.SimpleTimeZone[id=America/New_York,offset=-18000000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=3,startDay=1,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2004,MONTH=9,WEEK_OF_YEAR=44,WEEK_OF_MONTH=5,DAY_OF_MONTH=25,DAY_OF_YEAR=299,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=4,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=14,SECOND=2,MILLISECOND=474,ZONE_OFFSET=-18000000,DST_OFFSET=3600000]

Using DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.MEDIUM).getCalendar():
Zone Offset:  -18000000
DST Offset:   0
Year:         1924
Current Time: Sat Oct 25 00:14:02 EST 1924
java.util.GregorianCalendar[time=-1425926757436,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="America/New_York",offset=-18000000,dstSavings=3600000,useDaylight=true,transitions=235,lastRule=java.util.SimpleTimeZone[id=America/New_York,offset=-18000000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=3,startDay=1,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=1924,MONTH=9,WEEK_OF_YEAR=43,WEEK_OF_MONTH=4,DAY_OF_MONTH=25,DAY_OF_YEAR=299,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=4,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=14,SECOND=2,MILLISECOND=564,ZONE_OFFSET=-18000000,DST_OFFSET=0]
</pre>

Again, the year is wrong for DateFormat.getCalendar() and this time so is the DST_OFFSET.


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
<pre>
import java.text.*;
import java.util.*;

class CalendarTest {

    public static void main(String[] args) {
        Calendar c = Calendar.getInstance();
        System.out.println( "Using Calendar.getInstance():" );
        System.out.println( "Zone Offset:  " + c.get(Calendar.ZONE_OFFSET) );
        System.out.println( "DST Offset:   " + c.get(Calendar.DST_OFFSET) );
        System.out.println( "Year:         " + c.get(Calendar.YEAR) );
        System.out.println( "Current Time: " + c.getTime() );
        System.out.println( c );
        
        System.out.println();
        c = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.MEDIUM).getCalendar();
        System.out.println( "Using DateFormat.getDateTimeInstance(" +
                "DateFormat.FULL, DateFormat.MEDIUM).getCalendar():" );
        System.out.println( "Zone Offset:  " + c.get(Calendar.ZONE_OFFSET) );
        System.out.println( "DST Offset:   " + c.get(Calendar.DST_OFFSET) );
        System.out.println( "Year:         " + c.get(Calendar.YEAR) );
        System.out.println( "Current Time: " + c.getTime() );
        System.out.println( c );
    }

}
</pre>
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Don't use the DateFormat.getCalendar(), use Calendar.getInstance() instead.

Comments
EVALUATION This is a duplicate of 6609452.
19-10-2007