Unparsable Date with colon-separated timezone

南楼画角 提交于 2019-12-14 02:33:30

问题


I'm trying to parse this String

2014-04-04T14:28:38+02:00

It should be ISO 8601 format. But i can't parse it to a correct Date. I've tried the following:

   String example = "2014-04-04T14:28:38+02:00"
   public final static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz")
   Date tempDate = df.parse(example)

But I get always the message "unparseable Date" I can not change the example because it's a value from a webservice.

Could it be there is a probleme with "+02:00" instead of "+0200" ?

Thanks a lot


回答1:


Starting from Java7, to handle +02:00, you can use the following format:

"yyyy-MM-dd'T'hh:mm:ssXXX"

This can be seen in the SimpleDateFormat documentation




回答2:


The ISO8601 date format can be most easily parsed using Joda Time which can be use as a library.

It seems that a form of Joda Time got into Java 8 as JSR310 but I have not worked with it.



来源:https://stackoverflow.com/questions/22864571/unparsable-date-with-colon-separated-timezone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!