Joda DateTime:具有“CEST”的解析日期在字符串 [英] Joda DateTime: Parse date with "CEST" in string

查看:145
本文介绍了Joda DateTime:具有“CEST”的解析日期在字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用joda库解析一个DateTime(在scala中,应该和java一样)。

I want to parse a DateTime using the joda library (in scala, but should be the same with java).

源代码看起来像

val dateParser = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss z")
val date = dateParser.parseDateTime("2012-08-28 15:35:00 CEST")

在Java中,这看起来像

In Java this would look like

DateTimeFormatter dateParser = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss z");
DateTime date = dateParser.parseDateTime("2012-08-28 15:35:00 CEST");

不幸的是,这不起作用,但抛出一个例外:

Unfortunately this doesn't work but throws an exception:

java.lang.IllegalArgumentException: Invalid format: "2012-08-28 15:35:00 CEST" is malformed at "CEST"

为什么这不工作?当然,我可以忽略CEST部分,只是解析其余部分,但在输入CEST中有时候可以替换为CET,所以我不想忽略它。

Why doesn't this work? Of course I could ignore the "CEST" part and just parse the rest, but in the input "CEST" can be replaced by "CET" sometimes, so I don't want to ignore it.

推荐答案

问题是因为 CEST 是一个不是唯一的短区域名称。请参阅 DateTimeFormatter Bug解析与美国地区的一般时区的详细讨论?

The issue is because CEST is a short zone name that is not unique. See the detailed discussion from DateTimeFormatter Bug parsing with general time zones in US Locale?

您可以使用最新版本从,请参阅此提交

You can use the latest version from github that should contain a fix, see this commit.

您应该可以使用 DateTimeFormatterBuilder#appendTimeZoneShortName 添加 CEST 作为美国/芝加哥的别名,或您想如何解释 CEST

You should be able to use DateTimeFormatterBuilder#appendTimeZoneShortName to add CEST as an alias for "America/Chicago" or to how you would like to interpret CEST.

这篇关于Joda DateTime:具有“CEST”的解析日期在字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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