Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
893 views
in Technique[技术] by (71.8m points)

java - Joda DateTime: Parse date with "CEST" in string

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

The source looks like

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

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"

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...