JDK-6644489 : SimpleDateFormat.parse changes the TimeZone of the SimpleDateFormat object.
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.text
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2007-12-20
  • Updated: 2010-07-29
  • Resolved: 2008-04-30
Related Reports
Duplicate :  
Description
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.

Comments
EVALUATION Closing this CR as a duplicate of 6609675.
30-04-2008