SimpleDateFormat is throwing an exception in non-lenient mode when parsing strings that only specify a (legitimate) time that happens to be DST. This behavior is not backwards-compatible, and contradicts an example in the SimpleDateFormat JavaDoc.
TEST CODE -----------------------------------------
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.TimeZone;
public class TZTest {
public static void main(String[] args) throws Exception {
GregorianCalendar cal = new GregorianCalendar(2005, 7, 1, 12, 00);
TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
cal.setTimeZone(tz);
SimpleDateFormat df = new SimpleDateFormat("hh 'o''clock' a, zzzz");
df.setTimeZone(tz);
String formatted = df.format(cal.getTime());
System.out.println(formatted);
System.out.println("Lenient parsed date: " + df.parse(formatted));
df.setLenient(false);
System.out.println("Strict parsed date: " + df.parse(formatted));
}
}
-------------------------------------
The output from this program is:
12 o'clock PM, Pacific Daylight Time
Lenient parsed date: Thu Jan 01 11:00:00 PST 1970
Exception in thread main
java.text.ParseException: Unparseable date: "12 o'clock PM, Pacific Daylight Time"
at java.text.DateFormat.parse(DateFormat.java:335)
at TZTest.main(TZTest.java:21)
The ParseException is being thrown because the date defaults to January 1, which does not match with the "Pacific Daylight Time" timezone in the parsed string. This exception also appears if the timezone is set to "America/New York".
This behavior is new in 1.5, and breaks backward compatibility with applications written in 1.4.
The JavaDoc examples for SimpleDateParser include the pair "hh 'o''clock' a, zzzz"/ "12 o'clock PM, Pacific Daylight Time", which is identical to the above test code.
Suggested fix:
Default the date to the beginning of the daylight savings period in 1970 (e.g. 04 Apr 1970) for strings which specify a time and DST timezone, but not a date.
TEST CODE -----------------------------------------
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.TimeZone;
public class TZTest {
public static void main(String[] args) throws Exception {
GregorianCalendar cal = new GregorianCalendar(2005, 7, 1, 12, 00);
TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
cal.setTimeZone(tz);
SimpleDateFormat df = new SimpleDateFormat("hh 'o''clock' a, zzzz");
df.setTimeZone(tz);
String formatted = df.format(cal.getTime());
System.out.println(formatted);
System.out.println("Lenient parsed date: " + df.parse(formatted));
df.setLenient(false);
System.out.println("Strict parsed date: " + df.parse(formatted));
}
}
-------------------------------------
The output from this program is:
12 o'clock PM, Pacific Daylight Time
Lenient parsed date: Thu Jan 01 11:00:00 PST 1970
Exception in thread main
java.text.ParseException: Unparseable date: "12 o'clock PM, Pacific Daylight Time"
at java.text.DateFormat.parse(DateFormat.java:335)
at TZTest.main(TZTest.java:21)
The ParseException is being thrown because the date defaults to January 1, which does not match with the "Pacific Daylight Time" timezone in the parsed string. This exception also appears if the timezone is set to "America/New York".
This behavior is new in 1.5, and breaks backward compatibility with applications written in 1.4.
The JavaDoc examples for SimpleDateParser include the pair "hh 'o''clock' a, zzzz"/ "12 o'clock PM, Pacific Daylight Time", which is identical to the above test code.
Suggested fix:
Default the date to the beginning of the daylight savings period in 1970 (e.g. 04 Apr 1970) for strings which specify a time and DST timezone, but not a date.