Java/将 ISO-8601 (2010-12-16T13:33:50.513852Z) 转换为 Date 对象 [英] Java / convert ISO-8601 (2010-12-16T13:33:50.513852Z) to Date object

查看:22
本文介绍了Java/将 ISO-8601 (2010-12-16T13:33:50.513852Z) 转换为 Date 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何解析

<小时>

关于java.time

ThreeTen-Extra 项目使用附加类扩展了 java.time.该项目是未来可能添加到 java.time 的试验场.您可能会在这里找到一些有用的类,例如 间隔YearWeek, YearQuarter更多.

How to parse a String in ISO 8601 format with Zulu time?

javax.xml.bind.DatatypeConverter.parseDateTime("2010-12-16T13:33:50.513852Z")

returns

IllegalArgumentException: '2010-12-16T13:33:50.513852Z' weist ein falsches Format auf.

Which mean something like wrong format, anyone have a clue what iss wrong in here?

解决方案

tl;dr

Instant.parse( "2010-12-16T13:33:50.513852Z" )


java.time

The newer java.time classes can handle this string input.

The Z on the end is short for Zulu and means UTC, an offset of zero +00:00.

Instant

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 instant = Instant.parse( "2010-12-16T13:33:50.513852Z" );

Time zone

You may want to apply a time zone ZoneId to get a ZonedDateTime. Search Stack Overflow for those class names to learn more, as well as for classes OffsetDateTime and DateTimeFormatter.

Conversion

Best to avoid the troublesome old legacy class of java.util.Date. But if you insist, call the new conversion methods added to the old classes.

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


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

这篇关于Java/将 ISO-8601 (2010-12-16T13:33:50.513852Z) 转换为 Date 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆