Unexpected interaction between Calendar's set() and setTimeZone() method

XMLWordPrintable

    • Type: Bug
    • Resolution: Not an Issue
    • Priority: P4
    • None
    • Affects Version/s: 1.2.1
    • Component/s: core-libs



      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)
      ======================================================================

            Assignee:
            Masayoshi Okutsu
            Reporter:
            Dave Blair (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: