threetenbp:解析具有时区名称的日期时解析异常 [英] threetenbp: Parse exception when parsing date with time-zone name

查看:488
本文介绍了threetenbp:解析具有时区名称的日期时解析异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以EEE,dd MMM yyyy HH:mm:ss zzz格式解析日期,例如,使用Threeten的DateTimeFormatter之类的字符串,例如"Tue,16 May 2017 07:44:48 GMT".但是,由于某些原因,似乎无法解析时区名称(我试图在没有时区名称部分的情况下解析相同的字符串,并且起作用了.)

这是代码的解析部分:

DateTimeFormatter parseFormatter = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
ZonedDateTime parsedDate = ZonedDateTime.parse(date, parseFormatter);

然后出现以下错误:

org.threeten.bp.format.DateTimeParseException:Text'Tue,2017年5月16日 13:02:16 GMT'无法在索引26处解析

我为时区名称部分尝试了各种不同的格式(例如z,zzz,Z,ZZZ),但没有任何效果.同样,如果我解析不带时区名称部分的子字符串日期(转换为LocalDateTime),则它可以工作,因此我确定时区名称存在问题.有谁知道可能是什么问题?

解决方案

我不知道您的代码为什么不起作用.当我在Java 8中使用java.time类时,它就会执行此操作.因此,这只是对可能的修复方法的猜测:

    DateTimeFormatter parseFormatter = DateTimeFormatter.RFC_1123_DATE_TIME;
    ZonedDateTime parsedDate = ZonedDateTime.parse(date, parseFormatter);

您会发现,在相同的步骤上,这是一个略微的简化.

DateTimeFormatter.RFC_1123_DATE_TIME 记录为

返回RFC-1123日期时间格式,例如'Tue,3 Jun 2008 11:05:30 GMT'.

所以我认为它应该接受GMT作为时区名称.我应该说它适合您的日期字符串,并且它也可以在我的计算机上使用.我相信无论使用哪种语言环境,该格式化程序都会在星期和月份中使用英文缩写(或者您可以尝试使用DateTimeFormatter.RFC_1123_DATE_TIME.withLocale(Locale.ENGLISH),但是我真的没有必要).

也就是说,他们说您应该避免使用三个字母和四个字母的时区缩写.有些模棱两可,有些不是全时区,这会导致进一步的歧义.尽管GMT并不是最危险的解决方案,但解决问题的坚决解决方案是,如果您可以使用带偏移的日期字符串(例如+00:00或仅仅是Z)而不是三个字母的区域名称来获取日期字符串.

从问题"和此答案中看到两个示例,在IdeOne.com上实时运行.两者都成功.

I am trying to parse dates in the format of EEE, dd MMM yyyy HH:mm:ss zzz, so for example strings like "Tue, 16 May 2017 07:44:48 GMT" using threeten's DateTimeFormatter. However, it seems that the time-zone name can't get parsed for some reason (I tried to parse the same string just without the time-zone name part and that worked).

Here is the parsing part of the code:

DateTimeFormatter parseFormatter = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
ZonedDateTime parsedDate = ZonedDateTime.parse(date, parseFormatter);

And I get the following error:

org.threeten.bp.format.DateTimeParseException: Text 'Tue, 16 May 2017 13:02:16 GMT' could not be parsed at index 26

I tried all sorts of different formats for the time-zone name part (e.g. z, zzz, Z, ZZZ), but nothing worked. Again, if I parse a substringed date without the time-zone name part (into LocalDateTime), then it works, so I am sure that the problem is somehow with the time-zone name. Does anyone have any idea what the problem might be?

解决方案

I don’t know why your code didn’t work. It does when I use the java.time classes in my Java 8. So this is just a guess at a possible fix:

    DateTimeFormatter parseFormatter = DateTimeFormatter.RFC_1123_DATE_TIME;
    ZonedDateTime parsedDate = ZonedDateTime.parse(date, parseFormatter);

You will notice it is a slight simplification at the same tine.

DateTimeFormatter.RFC_1123_DATE_TIME is documented as

Returns the RFC-1123 date-time formatter, such as 'Tue, 3 Jun 2008 11:05:30 GMT'.

So I figure it should accept GMT as a time zone name. I should say it fits your date string, and it too works on my computer. I believe this formatter uses English abbreviations for day of week and month no matter the locale (or you could try DateTimeFormatter.RFC_1123_DATE_TIME.withLocale(Locale.ENGLISH), but I really don’t think it would be necessary).

That said, they say you should avoid the three and four letter time zone abbreviations. Some are ambiguous and some are not full time zones, which leads to further ambiguity. While GMT is not the most dangerous one, a rock solid solution to your problem would be if you could get your date string with an offset, such as +00:00 or just Z, instead of the three letter zone name.

See both examples, from the Question and from this Answer, running live at IdeOne.com. Both succeed.

这篇关于threetenbp:解析具有时区名称的日期时解析异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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