Java 8 日期和时间 API - 解析 yyyy-MM-dd'T'HH:mm:ss.SSSZ

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29938237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:08:59  来源:igfitidea点击:

Java 8 Date and Time API - parse yyyy-MM-dd'T'HH:mm:ss.SSSZ

javajava-8java-time

提问by vehovmar

I'm trying to parse date in ISO8601 format:

我正在尝试以 ISO8601 格式解析日期:

yyyy-MM-dd'T'HH:mm:ss.SSSZ

yyyy-MM-dd'T'HH:mm:ss.SSSZ

Am I correct that it is not possible to parse it with any of the default formats defined in java.time.format.DateTimeFormatter?

我是否正确无法使用java.time.format.DateTimeFormatter 中定义的任何默认格式解析它?

For example ISO_OFFSET_DATE_TIME will parse only:

例如 ISO_OFFSET_DATE_TIME 将仅解析:

yyyy-MM-dd'T'HH:mm:ss.SSSZZ

yyyy-MM-dd'T'HH:mm:ss.SSSZZ

Samples:

样品:

yyyy-MM-dd'T'HH:mm:ss.SSSZ
2015-04-29T10:15:00.500+0000

yyyy-MM-dd'T'HH:mm:ss.SSSZZ
2015-04-29T10:15:00.500+00:00

BTW:I know I can define my own formatter that is not the issue. Just wanted to ensure that I'm not missing something as the ISODateTimeFormatof Jodais able to parse both:

顺便说一句:我知道我可以定义自己的格式化程序,这不是问题。只是想确保我不会失去了一些东西作为ISODateTimeFormat乔达能够解析两种:

 org.joda.time.format.DateTimeFormatter dateTimeFormatter = ISODateTimeFormat.dateTime();
 DateTime dateTime = dateTimeFormatter.parseDateTime("2015-04-29T10:15:00.500+0000");

采纳答案by sulin

You should never be bothered by those annoying date-format.

你永远不应该被那些烦人的日期格式所困扰。

There has an new library dateparser.

有一个新的库dateparser

It can recognize any Stringautomatically, and parse it into Date, Calendar, LocalDateTime, OffsetDateTimecorrectly.

它可以自动识别任何字符串,并将其正确解析为DateCalendarLocalDateTimeOffsetDateTime

With it, you don't have to prepare any appropriate patterns like yyyy-MM-dd'T'HH:mm:ss.SSSZor yyyy-MM-dd'T'HH:mm:ss.SSSZZ:

有了它,您不必准备任何适当的模式,例如yyyy-MM-dd'T'HH:mm:ss.SSSZyyyy-MM-dd'T'HH:mm:ss.SSSZZ

Date date = DateParserUtils.parseDate("2015-04-29T10:15:00.500+0000");
Calendar calendar = DateParserUtils.parseCalendar("2015-04-29T10:15:00.500Z");
LocalDateTime dateTime = DateParserUtils.parseDateTime("2015-04-29 10:15:00.500 +00:00");

All works fine, please enjoy it.

一切正常,请尽情享受。

回答by nantitv

I am not sure this is your expected answer.

我不确定这是您期望的答案。

Method 1

方法一

Parse using Instant

使用 Instant 解析

Instant.parse("2015-06-28T10:13:14.743Z");

Method 2

方法二

The given input format is equivalent to ISO_DATE_TIME format after removing 'Z' from the given pattern yyyy-MM-dd'T'HH:mm:ss.SSSZ

从给定模式 yyyy-MM-dd'T'HH:mm:ss.SSSZ 中删除 'Z' 后,给定的输入格式等效于 ISO_DATE_TIME 格式

Then we can parse it using ISO_DATE_TIME

然后我们可以使用 ISO_DATE_TIME 解析它

 text = "2015-06-28T10:13:14.743"
 LocalDateTime.parse(text,DateTimeFormatter.ISO_DATE_TIME)

回答by llogiq

You are correct in that this does not appear to match any of the default formats, so you would need to build your format with a java.time.format.DateTimeFormatterBuilder.

您是正确的,因为这似乎与任何默认格式都不匹配,因此您需要使用java.time.format.DateTimeFormatterBuilder.

回答by Kh?i Hoàng Nguy?n

Joda uses ISO8601 formats for default ISODateTimeFormat. So based on the document http://www.w3.org/TR/NOTE-datetime, Joda would not be able to parse your format.

Joda 使用 ISO8601 格式作为默认的 ISODateTimeFormat。因此,根据文档http://www.w3.org/TR/NOTE-datetime,Joda 将无法解析您的格式。