Formatter in DateTimeFormatter for ISO 8601 date format of the time

dcalap

I have a question concerning DateTimeFormatter formatters.

In the SWAPI (https://swapi.co/documentation#people) you can read for the created and edited dates, that the format is something like this:

2014-12-09T13:50:51.644000Z

And the description for this dates is: enter image description here

But in the doc of Class DateTimeFormatter in Predefined Formatters section, I can't see any formatter that matches the SWAPI dates example.

The problem is, when I try to parse it, is working with SSSSSS:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'")

And with nnnnnn:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.nnnnnn'Z'")

Any idea of which one is the correct formatter?

Ole V.V.

Depending on your situation you may not need to specify a formatter at all. The classes of java.time parse (and print) ISO 8601 format as their default, that is, without any explicit formatter. As the description in your image says, this is the format you’ve got.

However two of the predefined formatters that you are linking to do match your example:

  1. ISO_INSTANT
  2. ISO_OFFSET_DATE_TIME

Which one to use depends on any requirements for the type you want to parse the string into. It’s simplest to parse into an Instant. Don’t specify a formatter, just use Instant.parse:

    String swapiCreatedString = "2014-12-09T13:50:51.644000Z";
    Instant created = Instant.parse(swapiCreatedString);
    System.out.println("Created " + created);

Output:

Created 2014-12-09T13:50:51.644Z

I you need to manipulate the parsed datetime further, for example format it for a user, OffsetDateTime offers more possibilities:

    OffsetDateTime created = OffsetDateTime.parse(swapiCreatedString);

Again no formatter is needed for parsing. Output is identical to the above.

I guess that the reasons why you didn’t see that the two mentioned formatters match include:

  • None of the examples include fraction of second, but the formatters accept between 0 and 9 decimals (inclusive) of fraction of second.
  • The example offset date-time has offset +01:00. You couldn’t know that Z works as an offset too. It’s pronounced “Zulu” and denotes UTC.

To answer your question more directly: None of your formats are quite correct. Since as I said Z is an offset, you will want to parse it as such and not as a literal so you get the offset information from the string. Without it you wouldn’t know at which offset to interpret the 13:50:51.644. .SSSSSS is correct for fraction of second while .nnnnnn means nanosecond of second and is incorrect here. There are 10^9 nanoseconds in a second, so n only works if there are 9 digits of nanoseconds. Maybe your own example gives the best illustration:

    // nnnnnn is incorrect
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.nnnnnn'Z'");
    String swapiCreatedString = "2014-12-09T13:50:51.644000Z";
    LocalDateTime created = LocalDateTime.parse(swapiCreatedString, formatter);
    System.out.println("Created " + created);

Created 2014-12-09T13:50:51.000644

You see that the 51.644 seconds have been incorrectly changed to 51.000644.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Spring @RequestParam DateTime format as ISO 8601 Date Optional Time

分類Dev

Spring @RequestParam DateTime format as ISO 8601 Date Optional Time

分類Dev

Epoch or iso8601 date format?

分類Dev

Converting a time String to ISO 8601 format

分類Dev

Deserialize "Zulu" time in ISO8601 format in jackson

分類Dev

DateTimeFormatterおよびISO 8601文字列

分類Dev

BigQuery-iso8601の週のformat_dateと年

分類Dev

How to parse a ISO 8601 duration format in Swift?

分類Dev

java.time.format.DateTimeFormatter.ISO_INSTANTのためのフォーマット文字列?

分類Dev

DateTimeFormatter.ISO_OFFSET_DATE_TIMEが期待どおりに機能しない

分類Dev

Date Time formatter gives incorrect date

分類Dev

Parsing ISO 8601 date format like 2015-06-27T13:16:37.363Z in Java

分類Dev

Parsing ISO 8601 date format like 2015-06-27T13:16:37.363Z in Java

分類Dev

Parsing ISO 8601 duration format to Joda duration - IllegalArgumentException

分類Dev

How to parse and generate DateTime objects in ISO 8601 format

分類Dev

How to parse input string in standard ISO 8601 format in UTC to Java?

分類Dev

Return dates in ISO-8601 format - Debezium/Postgres plugin

分類Dev

DateTimeFormatter.ISO_OFFSET_DATE_TIMEの同等のフォーマット文字列は何ですか?

分類Dev

Java DateTimeFormatter.ISO_OFFSET_DATE_TIMEは、Java 8以降と比較して、Java 9以降で異なる値を返します。

分類Dev

ISO8601 Date Strings in MongoDB (Indexing and Querying)

分類Dev

PHP $date->format(DateTime::ISO8601) 異なるタイムゾーンオフセットを返す

分類Dev

Java 8 Date&Time API:特定の月のISO8601週のカウントを取得する方法は?

分類Dev

java.io.NotSerializableException:java.time.format.DateTimeFormatter

分類Dev

DateTimeFormatterを使用したjava.time.format.DateTimeParseException

分類Dev

What is this date and time format?

分類Dev

Java 8以降のDateTimeFormatter.ISO_LOCAL_DATEとDateTimeFormatter.ofPattern( "yyyy-MM-dd")

分類Dev

オプション付きのLocalDateTimeにISO_DATE_TIME.format()オフセット

分類Dev

Does ISO-8601 allow the time-zone abbreviation UTC rather than Z as the time-zone?

分類Dev

How do I convert a Calendar object to an ISO 8601 format DateTime string?

Related 関連記事

  1. 1

    Spring @RequestParam DateTime format as ISO 8601 Date Optional Time

  2. 2

    Spring @RequestParam DateTime format as ISO 8601 Date Optional Time

  3. 3

    Epoch or iso8601 date format?

  4. 4

    Converting a time String to ISO 8601 format

  5. 5

    Deserialize "Zulu" time in ISO8601 format in jackson

  6. 6

    DateTimeFormatterおよびISO 8601文字列

  7. 7

    BigQuery-iso8601の週のformat_dateと年

  8. 8

    How to parse a ISO 8601 duration format in Swift?

  9. 9

    java.time.format.DateTimeFormatter.ISO_INSTANTのためのフォーマット文字列?

  10. 10

    DateTimeFormatter.ISO_OFFSET_DATE_TIMEが期待どおりに機能しない

  11. 11

    Date Time formatter gives incorrect date

  12. 12

    Parsing ISO 8601 date format like 2015-06-27T13:16:37.363Z in Java

  13. 13

    Parsing ISO 8601 date format like 2015-06-27T13:16:37.363Z in Java

  14. 14

    Parsing ISO 8601 duration format to Joda duration - IllegalArgumentException

  15. 15

    How to parse and generate DateTime objects in ISO 8601 format

  16. 16

    How to parse input string in standard ISO 8601 format in UTC to Java?

  17. 17

    Return dates in ISO-8601 format - Debezium/Postgres plugin

  18. 18

    DateTimeFormatter.ISO_OFFSET_DATE_TIMEの同等のフォーマット文字列は何ですか?

  19. 19

    Java DateTimeFormatter.ISO_OFFSET_DATE_TIMEは、Java 8以降と比較して、Java 9以降で異なる値を返します。

  20. 20

    ISO8601 Date Strings in MongoDB (Indexing and Querying)

  21. 21

    PHP $date->format(DateTime::ISO8601) 異なるタイムゾーンオフセットを返す

  22. 22

    Java 8 Date&Time API:特定の月のISO8601週のカウントを取得する方法は?

  23. 23

    java.io.NotSerializableException:java.time.format.DateTimeFormatter

  24. 24

    DateTimeFormatterを使用したjava.time.format.DateTimeParseException

  25. 25

    What is this date and time format?

  26. 26

    Java 8以降のDateTimeFormatter.ISO_LOCAL_DATEとDateTimeFormatter.ofPattern( "yyyy-MM-dd")

  27. 27

    オプション付きのLocalDateTimeにISO_DATE_TIME.format()オフセット

  28. 28

    Does ISO-8601 allow the time-zone abbreviation UTC rather than Z as the time-zone?

  29. 29

    How do I convert a Calendar object to an ISO 8601 format DateTime string?

ホットタグ

アーカイブ