JDK-4227735 : Unexpected interaction between Calendar's set() and setTimeZone() method
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util:i18n
  • Affected Version: 1.2.1
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: generic
  • CPU: generic
  • Submitted: 1999-04-07
  • Updated: 1999-05-31
  • Resolved: 1999-05-31
Related Reports
Relates :  
Description

Name: dbT83986			Date: 04/07/99


The following program sets the HOUR_OF_DAY of a Calendar object to a value supplied in the command line arguments. Then it sets the
timezone to GMT. The HOUR_OF_DAY field is not updated unless there is an extra cal.setTime(cal.getTime()) before the timezone change.

import java.util.*;

public class Test {
  public static void main(String args[]) {
    Calendar cal = Calendar.getInstance();
    System.out.println(cal.get(Calendar.HOUR_OF_DAY));
    cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(args[0]));

    // If the line
    //cal.get(Calendar.HOUR_OF_DAY);
    // or the line
    //cal.setTime(cal.getTime());
    // is inserted here, then the result will be correct.

    cal.setTimeZone(TimeZone.getTimeZone("GMT"));
    // The following line is needed to update the fields after
    // a timezone change.
    cal.setTime(cal.getTime());

    // The following result is incorrect.
    System.out.println(cal.get(Calendar.HOUR_OF_DAY));
  }
}


C:\Imedia>java -version
java version "1.2.1"
Classic VM (build JDK-1.2.1-A, native threads)

C:\Imedia>java -fullversion
java full version "JDK-1.2.1-A"
(Review ID: 56651) 
======================================================================

Comments
WORK AROUND Name: dbT83986 Date: 04/07/99 Do an extra cal.setTime(cal.getTime()) before the timezone change. ======================================================================
11-06-2004

EVALUATION A value set using the Calendar set methods, including setTimeZone(), takes effect when it's necessary to calculate all fields, such as getTime(). The specification is not clear at all, though. masayoshi.okutsu@Eng 1999-05-30
30-05-1999