适用于W3C XML的Java SimpleDateFormat模式日期与时区 [英] Java SimpleDateFormat pattern for W3C XML dates with timezone
问题描述
我正在尝试解析 W3C XML Schema日期,如下所示
I am trying to parse a W3C XML Schema date like the following
"2012-05-15T07:08:09+03:00"
符合ISO 8601版本的 W3C XML Schema dateTime 规范
which complies with the ISO 8601 version of the W3C XML Schema dateTime specification.
在上述日期,时区标识符为+ 03:00
,但是没有 SimpleDateFormat
模式显然存在来表示它。
In the above date, the timezone identifier is "+03:00"
, but no SimpleDateFormat
pattern apparently exists to represent it.
如果时区为+ 0300
,则 Z
(大写)将适用,并且 SimpleDateFormat
模式将是
If the timezone were "+0300"
, then Z
(uppercase) would be applicable and the SimpleDateFormat
pattern would be
yyyy-MM-dd'T'HH:mm:ssZ
同样,如果时区为GMT + 03:00
,则 z
(小写)将适用,并且 SimpleDateFormat
模式将是
Similarly, if the timezone were "GMT+03:00"
, then z
(lowercase) would be applicable and the SimpleDateFormat
pattern would be
yyyy-MM-dd'T'HH:mm:ssz
(大写 Z
也可以正常工作)。
(uppercase 'Z'
also works, by the way).
所以,是否有一个 SimpleDateFormat
模式或解决方法来表示上述日期而不预处理日期字符串?
So, is there a SimpleDateFormat
pattern or workaround to represent the above date without preprocessing of the date string?
推荐答案
如何如下:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"){
public Date parse(String source,ParsePosition pos) {
return super.parse(source.replaceFirst(":(?=[0-9]{2}$)",""),pos);
}
};
这篇关于适用于W3C XML的Java SimpleDateFormat模式日期与时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!