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

(tz) Epoch GMT offset causes unexpected behaviour in SimpleDateFormat parsing

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 5.0
    • core-libs
    • x86
    • windows_xp

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

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      GMT timezone

      A DESCRIPTION OF THE PROBLEM :
      Olsen states that at the time of the epoch, the GMT offset was +1. This means that Dates created as "new Date(0L)" appear as "Thu Jan 01 01:00:00 GMT 1970".

      However, date-parsing via SimpleDateFormat does not take this offset into account when interpreting dates either to or from Strings, resulting in unexpected and (believed) incorrect behaviour.

      (Please see bug report 4832236 for background to the new Date(0L) behaviour.)


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Construct a Date at the epoch. Format this Date as a String, then parse this String back into a Date - the date will have shifted by +1 hour.

      The attached test-case constructs a Date at the epoch, then repeatedly transforms it to a String and back.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Even though it was converted via a String-representation, the date should not have changed.
      ACTUAL -
      Each format/parse cycle shifts the date by +1 hour. Exact output from test-case is as follows:

      Thu Jan 01 01:00:00 GMT 1970
      Thu Jan 01 02:00:00 GMT 1970
      Thu Jan 01 03:00:00 GMT 1970
      Thu Jan 01 04:00:00 GMT 1970
      Thu Jan 01 05:00:00 GMT 1970
      Thu Jan 01 06:00:00 GMT 1970
      Thu Jan 01 07:00:00 GMT 1970
      Thu Jan 01 08:00:00 GMT 1970
      Thu Jan 01 09:00:00 GMT 1970
      Thu Jan 01 10:00:00 GMT 1970

      REPRODUCIBILITY :
      This bug can be reproduced always.

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

      public class EpochDateShiftTest {
      public static void main(String[] args) throws Exception {
      final SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
      Date date = new Date(0L);

      for (int i = 0; i < 10; i++) {
      System.out.println(date.toString());
      date = sdf.parse(sdf.format(date));
      }
      }
      }

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

            okutsu Masayoshi Okutsu
            okutsu Masayoshi Okutsu
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: