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

SimpleDateFormat.parse changes the TimeZone of the SimpleDateFormat object.

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_13"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_13-b05)
      Java HotSpot(TM) Client VM (build 1.5.0_13-b05, mixed mode)

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

      A DESCRIPTION OF THE PROBLEM :
      Parsing a date string with a SimpleDateFormat object can cause the TimeZone of the SimpleDateFormat object to be set to the one in the parsed string (the format string and the parsed string need to include a timezone). This seems wrong unless the purpose of the parse method is to set up the SimpleDateFormat object.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      See code sample.





      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I had expected a Date object to be returned without modification to the SimpleDateFormat object.
      ACTUAL -
      The SimpleDateFormat object TimeZone was changed to the one in the parsed String.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      No logs

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      java.text.SimpleDateFormat sdf = new java.text.SimpleDateForma("MMM d HH:mm:ss.SSS z yyyy");
      String timestamp = "OCT 1 12:00:00.000 MST 2000";
      java.util.TimeZone tz = java.util.TimeZone.getTimeZone("GMT");
      java.util.Date now = new java.util.Date(970426800000L);
      sdf.setTimeZone(tz);

      System.out.println(sdf.getTimeZone().getID());
      System.out.println(sdf.format(now));

      java.util.Date date = sdf.parse(timestamp);
      System.out.println(date.getTime());

      System.out.println(sdf.getTimeZone().getID());
      System.out.println(sdf.format(now));
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Extending the SimpleDateFormat object and overriding the setTimeZone method can prevent the time zone from being manipulated by the parse method.

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

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: