I'm on America/Los_Angeles
TZ, and when I try to render midnight in the America/Mazatlan
TZ, I get the following exception:
Exception in thread "main" java.text.ParseException: Unparseable date: "12:00 AM"
Here is my code to reproduce this:
DateFormat dateFormat = new SimpleDateFormat("h:mm a");
TimeZone timeZone = TimeZone.getTimeZone("America/Mazatlan");
dateFormat.setTimeZone(timeZone);
dateFormat.setLenient(false);
Date parse = dateFormat.parse("12:00 AM");
I'm aware the commenting out the setLenient(false)
will fix the issue, I'm just not sure why that is a fix as other timezones in the same offset, such as America/Inuvik
do not cause issues like this.
Any assistance would be great.
When you don't specify a date, 1970-01-01 is used.
The time zone definition for Mazatlan shows that the base offset switched from -08:00 to -07:00 in 1970. This creates a discontinuity in local time, similar to the kind usually found during a spring-forward daylight saving time transition.
There is an hour of missing local time, from midnight to just before 1:00. Times in this range are invalid. Assuming the zone definition is correct, that means the clocks ticked forward like this:
Therefore, if you are using
SimpleDateFormat
- you should include a date, not just a time.That because you
dateFormat.setLenient(false);
and 12:00 should be 'PM' not 'AM'If you remove the line,
Your parse object value is becoming
I don't know why but for
America/Mazatlan TZ
this line is creating exception.For
America/Los_Angeles TZ
andAmerica/Inuvik TZ
, usage ofdateFormat.setLenient(false)
line not giving any error and results are the same withAmerica/Mazatlan TZ
.