如何将XML date(string)转换为Date? [英] How to convert XML date (string) to Date?
问题描述
2008-11-10T05:51:33Z
的标签,我想将这个字符串转换为 java。 util.Date对象。
如何才能完成?
使用 java.text .DateFormat - 或者更有可能是 SimpleDateFormat 。
或者,去 Joda时间与其无限更好的API。注意Java内置的API - DateFormats不是线程安全的。 (他们在Joda时间,几乎在任何地方使用不变的类型。)
An(未测试 - 应该是正常的,除了可能时区位)例子为Joda Time API:
DateTimeFormatter fmt = DateTimeFormat.forPattern(yyyyMMdd'T'HH:mm:ssZ );
DateTime dt = fmt.parse(2008-11-10T05:51:33Z);
I am parsing XML file. In this file there is one tag containing date string "2008-11-10T05:51:33Z"
and I want convert this string in to java.util.Date object.
How can this be done?
Use java.text.DateFormat - or more likely, SimpleDateFormat.
Alternatively, go for Joda Time with its infinitely better API. Be careful with the Java built-in APIs - DateFormats aren't thread-safe. (They are in Joda Time, which uses immutable types almost everywhere.)
An (untested - should be fine except for possibly the timezone bit) example for the Joda Time API:
DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyyMMdd'T'HH:mm:ssZ");
DateTime dt = fmt.parse("2008-11-10T05:51:33Z");
这篇关于如何将XML date(string)转换为Date?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!