如何将ISO8601日期(字符串)转换为日期? [英] How to convert ISO 8601 date (string) to Date?
问题描述
我正在解析XML文件。在这个文件中有一个包含日期字符串2008-11-10T05:51:33Z
的标签,我希望将此字符串转换为 java。 util.Date对象。
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.
如何做到这一点?
推荐答案
使用 java.text .DateFormat - 或者更有可能, SimpleDateFormat 。
Use java.text.DateFormat - or more likely, SimpleDateFormat.
或者,请访问 Joda时间及其无限更好的API。小心Java内置API - DateFormats不是线程安全的。 (它们在Joda Time中,几乎在任何地方都使用不可变类型。)
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(未经测试 - 除了可能时区位之外应该没问题)Joda Time API的示例:
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");
这篇关于如何将ISO8601日期(字符串)转换为日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!