简体   繁体   English

如何将ISO8601日期(字符串)转换为日期?

[英]How to convert ISO 8601 date (string) to Date?

I am parsing XML file. 我正在解析XML文件。 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. 在这个文件中有一个包含日期字符串"2008-11-10T05:51:33Z"标记,我想将此字符串转换为java.util.Date object.

How can this be done? 如何才能做到这一点?

Use java.text.DateFormat - or more likely, SimpleDateFormat . 使用java.text.DateFormat - 或者更有可能的是SimpleDateFormat

Alternatively, go for Joda Time with its infinitely better API. 或者,使用其无限更好的API去Joda Time Be careful with the Java built-in APIs - DateFormats aren't thread-safe. 小心Java内置API - DateFormats不是线程安全的。 (They are in Joda Time, which uses immutable types almost everywhere.) (它们在Joda Time中,几乎在所有地方都使用不可变类型。)

An (untested - should be fine except for possibly the timezone bit) example for the Joda Time API: Joda Time API的示例(未经测试 - 除了可能的时区位之外应该没问题):

DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyyMMdd'T'HH:mm:ssZ");
DateTime dt = fmt.parse("2008-11-10T05:51:33Z");

tl;dr TL;博士

Instant.parse( "2008-11-10T05:51:33Z" ) 

Using java.time 使用java.time

The modern approach uses the java.time classes. 现代方法使用java.time类。 Avoid the troublesome older date-time classes such as Date and Calendar as they are now supplanted by java.time classes. 避免麻烦的旧日期时间类,如DateCalendar因为它们现在已被java.time类取代。

ISO 8601 ISO 8601

Your input string complies with the ISO 8601 standard for formatting strings representing date-time values. 您的输入字符串符合ISO 8601标准,用于格式化表示日期时间值的字符串。 The java.time classes use the standard formats by default when parsing/generating strings, so no need to specify a formatting pattern. 在解析/生成字符串时,java.time类默认使用标准格式,因此无需指定格式化模式。

The Z on the end is short for Zulu and means UTC. 最后的ZZulu缩写,意思是UTC。

The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction). Instant类表示UTC时间轴上的一个时刻,分辨率为纳秒 (最多九(9)位小数)。

Instant instant = Instant.parse( "2008-11-10T05:51:33Z" ) ;

java.util.Date

Best to avoid the old legacy class java.util.Date . 最好避免旧的遗留类java.util.Date But if you must, you can convert to/from java.time by calling new methods added to the old classes. 但是,如果必须,您可以通过调用添加到旧类的新方法来转换为/从java.time转换。 Here we use Date.from . 这里我们使用Date.from

java.util.Date d = java.util.Date.from( instant ) ;

About java.time 关于java.time

The java.time framework is built into Java 8 and later. java.time框架内置于Java 8及更高版本中。 These classes supplant the troublesome old legacy date-time classes such as java.util.Date , Calendar , & SimpleDateFormat . 这些类取代了麻烦的旧遗留日期时间类,如java.util.DateCalendarSimpleDateFormat

The Joda-Time project, now in maintenance mode , advises migration to the java.time classes. 现在处于维护模式Joda-Time项目建议迁移到java.time类。

To learn more, see the Oracle Tutorial . 要了解更多信息,请参阅Oracle教程 And search Stack Overflow for many examples and explanations. 并搜索Stack Overflow以获取许多示例和解释。 Specification is JSR 310 . 规范是JSR 310

You may exchange java.time objects directly with your database. 您可以直接与数据库交换java.time对象。 Use a JDBC driver compliant with JDBC 4.2 or later. 使用符合JDBC 4.2或更高版本的JDBC驱动程序 No need for strings, no need for java.sql.* classes. 不需要字符串,不需要java.sql.*类。

Where to obtain the java.time classes? 从哪里获取java.time类?

The ThreeTen-Extra project extends java.time with additional classes. ThreeTen-Extra项目使用其他类扩展了java.time。 This project is a proving ground for possible future additions to java.time. 该项目是未来可能添加到java.time的试验场。 You may find some useful classes here such as Interval , YearWeek , YearQuarter , and more . 您可以在这里找到一些有用的类,比如IntervalYearWeekYearQuarter ,和更多

DateTime dateToGetFromString;

dateToGetFromString = DateTime.Parse(stringContainingDate);

You can also use the TryParse function of DateTime class, look in the documentation. 您还可以使用DateTime类的TryParse函数,查看文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM