JDK-4177484 : Calendar.setTimeZone() doesn't update all fields
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util:i18n
  • Affected Version: 1.1.4,1.1.6,1.2.0
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic,solaris_2.6,windows_95
  • CPU: generic,x86
  • Submitted: 1998-09-30
  • Updated: 2019-09-13
  • Resolved: 1999-05-31
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
1.3.0 betaFixed
Related Reports
Duplicate :  
Relates :  
Relates :  
Description

Name: nl37777			Date: 09/29/98


The Calendar.setTimeZone does not update all of
the fields in the Calendar object.

Notice in the output below that when a Calendar
created with a PST timezone is converted to the
EST timezone, the ERA,YEAR,MONTH,WEEK_OF_YEAR,
WEEK_OF_MONTH,DAY_OF_MONTH,DAY_OF_YEAR,DAY_OF_WEEK,
DAY_OF_WEEK_IN_MONTH,AM_PM,HOUR,HOUR_OF_DAY,MINUTE,
SECOND,MILLISECOND,ZONE_OFFSET and DST_OFFSET
values are not updated.

This causes the Calendar.get(field) to return the
incorrect values 

Here's a sample program to illustrate the bug.

import java.io.*;
import java.util.*;


public class datebug
{

  public static void main(String argv[])
  {
    Calendar caltmp = Calendar.getInstance();
    Calendar calEST = Calendar.getInstance(TimeZone.getTimeZone("EST"));
    Calendar calPST = Calendar.getInstance(TimeZone.getTimeZone("PST"));

    caltmp.set(1998,8,28,23,0,0);    // Set the time

    Date date = caltmp.getTime();     // Create a date object

    System.out.println("Date : " + date);

    calEST.setTime(date);
    calPST.setTime(date);

    System.out.println("calEST Date : " + calEST.getTime() );
    System.out.println("calPST Date : " + calPST.getTime() );

    System.out.println("\ncalEST Calendar");
    System.out.println(calEST);

    System.out.println("\ncalPST Calendar");
    System.out.println(calPST);

    System.out.println("\nConvert PST to EST");

    calPST.setTimeZone(TimeZone.getTimeZone("EST"));

    System.out.println("\ncalPST Date : " + calPST.getTime());

    System.out.println("\ncalPST Calendar in EST");
    System.out.println(calPST);

  }
}

Here is the output from the above program : 

Date : Mon Sep 28 23:00:00 EDT 1998
calEST Date : Mon Sep 28 23:00:00 EDT 1998
calPST Date : Mon Sep 28 23:00:00 EDT 1998

calEST Calendar
java.util.GregorianCalendar[time=907038000558,areFieldsSet=true,areAllFieldsSet=true,
lenient=true,zone=java.util.SimpleTimeZone[id=EST,offset=-18000000,dstSavings=3600000,
useDaylight=true,startYear=0,startMode=3,startMonth=3,startDay=1,startDayOfWeek=1,
startTime=7200000,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=7200000],
firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=1998,MONTH=8,WEEK_OF_YEAR=40,
WEEK_OF_MONTH=5,DAY_OF_MONTH=28,DAY_OF_YEAR=271,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=4,
AM_PM=1,HOUR=11,HOUR_OF_DAY=23,MINUTE=0,SECOND=0,MILLISECOND=558,ZONE_OFFSET=-18000000,
DST_OFFSET=3600000]

calPST Calendar
java.util.GregorianCalendar[time=907038000558,areFieldsSet=true,areAllFieldsSet=true,
lenient=true,zone=java.util.SimpleTimeZone[id=PST,offset=-28800000,dstSavings=3600000,
useDaylight=true,startYear=0,startMode=3,startMonth=3,startDay=1,startDayOfWeek=1,
startTime=7200000,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=7200000],
firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=1998,MONTH=8,WEEK_OF_YEAR=40,
WEEK_OF_MONTH=5,DAY_OF_MONTH=28,DAY_OF_YEAR=271,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=4,
AM_PM=1,HOUR=8,HOUR_OF_DAY=20,MINUTE=0,SECOND=0,MILLISECOND=558,ZONE_OFFSET=-28800000,
DST_OFFSET=3600000]

Convert PST to EST

calPST Date : Mon Sep 28 23:00:00 EDT 1998

calPST Calendar in EST
java.util.GregorianCalendar[time=907038000558,areFieldsSet=true,areAllFieldsSet=true,
lenient=true,zone=java.util.SimpleTimeZone[id=EST,offset=-18000000,dstSavings=3600000,
useDaylight=true,startYear=0,startMode=3,startMonth=3,startDay=1,startDayOfWeek=1,
startTime=7200000,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=7200000],
firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=1998,MONTH=8,WEEK_OF_YEAR=40,
WEEK_OF_MONTH=5,DAY_OF_MONTH=28,DAY_OF_YEAR=271,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=4,
AM_PM=1,HOUR=8,HOUR_OF_DAY=20,MINUTE=0,SECOND=0,MILLISECOND=558,ZONE_OFFSET=-28800000,
DST_OFFSET=3600000]
(Review ID: 37904)
======================================================================

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: kestrel FIXED IN: kestrel-beta INTEGRATED IN: kestrel-beta
14-06-2004

WORK AROUND Name: nl37777 Date: 09/29/98 The simplest workaround I found was to create a new calendar with the desired time zone and set the time. ie. newCal = Calendar.getInstance(TimeZone.getTimeZone("EST")); newCal.setTime(calPST.getTime()); newCal nows contains the correct information. ======================================================================
11-06-2004

EVALUATION Should be fixed. masayoshi.okutsu@Eng 1999-04-14 Changed setTimeZone() to invalidate the fields so that the fields are recalculated later at getTime() call. Note that setTimeZone() follows the convention of the other "set" methods, i.e., timezone set by setTimeZone takes effect at getTime() call. masayoshi.okutsu@Eng 1999-05-30 Amendment to the above description: In case setting time zone doesn't affect the validity of the internal UTC time value, getTime() doesn't cause re-computation of the fields invalidated by setTimeZone(). toString() doesn't re-compute the fields, either. Therefore, the test program in the Description needs to call get() before printing a Calendar object to get all fields re-computed. masayoshi.okutsu@Eng 1999-10-19
19-10-1999