无法解释的日期在美国/马萨特兰时区 [英] Unparseable Date In America/Mazatlan timezone

查看:171
本文介绍了无法解释的日期在美国/马萨特兰时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 America / Los_Angeles TZ,当我尝试在 America / Mazatlan 中渲染午夜TZ,我得到以下异常:

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"

这是我的代码重现:

    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");

我知道评论 setLenient(false)将解决问题,我只是不确定为什么这是一个修复,因为同一偏移中的其他时区,如 America / Inuvik 不会导致问题像这样。

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.

任何帮助都会很棒。

推荐答案

当你不' t指定日期,使用1970-01-01。

When you don't specify a date, 1970-01-01 is used.

Mazatlan的时区定义显示基准偏移在1970年从-08:00切换到-07:00。这会在当地时间产生不连续性,类似通常在春季夏令时转换期间找到的那种。

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.

当地时间有一个小时从午夜到凌晨1点。此范围内的时间无效。假设区域定义正确,这意味着时钟向前勾选如下:

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:

======== 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

因此,如果您使用 SimpleDateFormat - 您应该包括日期,而不仅仅是时间。

Therefore, if you are using SimpleDateFormat - you should include a date, not just a time.

这篇关于无法解释的日期在美国/马萨特兰时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆