Unrivaled Date in America's Watches / Mazatlan

I am on America/Los_Angeles

TZ and when I try to make it midnight in 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 know a comment setLenient(false)

will fix the problem, I'm just not sure why this is a fix as other time zones at the same offset, such as America/Inuvik

, do not cause such problems.

Any help would be great.

+3


source to share


3 answers


If you do not specify a date, the period 1970-01-01 is used.

timezone definition for Mazatlan shows that the base offset switched from -08: 00 to -07: 00 in 1970. This creates a gap in local time, similar to that commonly found during spring-forward daylight savings time .

There is an hour of no local time, from midnight to 1:00. Times in this range are invalid. Assuming that the zone definition is correct, this means that the clock goes off forward as follows:



======== UTC =======     ==== America/Mazatlan ===
1970-01-01T07:59:57Z     1969-12-31T23:59:57-08:00
1970-01-01T07:59:58Z     1969-12-31T23:59:58-08:00
1970-01-01T07:59:59Z     1969-12-31T23:59:59-08:00   
1970-01-01T08:00:00Z     1970-01-01T01:00:00-07:00  (transition!)
1970-01-01T08:00:01Z     1970-01-01T01:00:01-07:00
1970-01-01T08:00:02Z     1970-01-01T01:00:02-07:00

      

Therefore, if you use SimpleDateFormat

- you must specify the date, not just the time.

+3


source


If you remove the line,

    dateFormat.setLenient(false);

      

Your parse object value becomes

     Thu Jan 01 10:00:00 EET 1970

      



I don't know why, but for America/Mazatlan TZ

this line it throws an exception.

For America/Los_Angeles TZ

and America/Inuvik TZ

using string dateFormat.setLenient(false)

does not give errors and results the same as America/Mazatlan TZ

.

    Thu Jan 01 10:00:00 EET 1970

      

+1


source


This is because you dateFormat.setLenient(false);

and 12:00 should be "PM" and not "AM"

-1


source







All Articles