Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4844441

In Calendar, the order of calls to setTimeZone() and setTime() affects getTime()

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.4.1
    • core-libs



      Name: jl125535 Date: 04/07/2003


      FULL PRODUCT VERSION :
      java version "1.4.1_01"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_01-b01)
      Java HotSpot(TM) Client VM (build 1.4.1_01-b01, mixed mode)

      FULL OPERATING SYSTEM VERSION :

      glibc-2.2-12

      Linux walrus.sockeye.com 2.2.19-7.0.1 #1 Tue Apr 10
      01:56:16 EDT 2001 i686 unknown

      Red Hat Linux release 7.0 (Guinness)


      A DESCRIPTION OF THE PROBLEM :

      If you change some Calendar fields by calling set(),
      the day will be off by one if you had called setTime()
      *before* calling setTimeZone(). If you call setTimeZone()
      as the first thing, there's no problem.

      This bug only happens for time zones east of GMT (or at
      least Europe/Paris).



      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Just reverse the two lines of code as the source code
      says.
      2.
      3.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      expected:

      before: 07 Nov 2002 01:00 CET
      after: 07 Nov 2002 00:00 CET

      After reversing the two lines noted, the following
      incorrect output is generated:

      before: 07 Nov 2002 01:00 CET
      after: 06 Nov 2002 00:00 CET

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.text.SimpleDateFormat;
      import java.util.Calendar;
      import java.util.Date;
      import java.util.TimeZone;

      /*

      This program normalizes a date to midnight in the CET time zone.
      The expected output is:

      before: 07 Nov 2002 01:00 CET
      after: 07 Nov 2002 00:00 CET

      After reversing the two lines noted, the following
      incorrect output is generated:

      before: 07 Nov 2002 01:00 CET
      after: 06 Nov 2002 00:00 CET
       */
      public class Bug {

          public static void main(String[] args) {
              TimeZone tz = TimeZone.getTimeZone("Europe/Paris");

              Date date = new Date(1036627200000L);

              Calendar cal = Calendar.getInstance();

              // Reversing the following two lines causes the
              // subsequent code to compute the wrong day (Nov 6 when
              // it should be Nov 7).
              cal.setTimeZone(tz);
              cal.setTime(date);

              SimpleDateFormat formatter =
                  new SimpleDateFormat("dd MMM yyyy HH:mm zzz");
              formatter.setTimeZone(tz);

              System.out.println("before: " + formatter.format(cal.getTime()));

              cal.set(Calendar.HOUR_OF_DAY, 0);
              cal.set(Calendar.MINUTE, 0);
              cal.set(Calendar.SECOND, 0);

              System.out.println("after: " + formatter.format(cal.getTime()));
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER WORKAROUND :
      Make sure you call setTimeZone() before calling
      setTime().
      (Review ID: 166807)
      ======================================================================

            okutsu Masayoshi Okutsu
            jleesunw Jon Lee (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: