Java Jsonb deserializing UTC datetime in ISO8601

Marco Del Percio :

I'm using JSON-B (yasson implementation) and I'm receiving data for an object with a field like this

{
  ...
  "timestamp": "2020-03-19T06:42:42Z",
  ...
}

which is perfectly standard ISO 8601 for a UTC date-time value. Now the corresponding Java class simply declares a Date member variable with no other specific annotation

...
 private Date timestamp;
...

everything seems to work fine and it looks like the JSON-B implementation correctly understands that as UTC without me having to specify a format using the @JsonbDateFormat annotation. I think I'm sure of it because I checked with

ZonedDateTime datetimeCheck = ZonedDateTime.of(2020, 3, 19, 6, 42, 42, 0, ZoneId.of("UTC"));
Date parsedDateFromJson = myModel.getTimestamp();
boolean compareTs = parsedDateFromJson.equals(Date.from(datetimeCheck.toInstant()));

and it yields true however, when I ran another test removing the 'Z' from the date-time value I was expecting it to produce a different result interpreting the date-time value as local rather than UTC. With my great surprise, the Date object obtained by JSON-B was exactly the same. What am I missing here? Why are 2020-03-19T06:42:42Z and 2020-03-19T06:42:42 the same thing? (I don't think they are). Or maybe is the JSON-B implementation considering UTC as default always when no timezone is specified?

Thanks

Martijn :

Or maybe is the JSON-B implementation considering UTC as default always when no timezone is specified?

Exactly this. Contrary to what its name implies, a java.util.Date actually doesn't represent a date but an instant in time. Specifically, it represents the number of milliseconds since UNIX epoch (January 1, 1970, 00:00:00 GMT).

2020-03-19T06:42:42 is a date + time without zone or offset information. When deserializing this date + time to a java.util.Date, i.e. an instant in time, you need to decide how to interpret the date + time. Is this the date + time at UTC? Is this the date + time in your local timezone?

Luckily for us, the JSON-B specification contains the following: "If not specified otherwise in this section, GMT standard time zone and offset specified from UTC Greenwich is used." As this statement shows, the authors made the choice of interpreting a date + time without timezone as a date + time at UTC.

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

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

編集
0

コメントを追加

0

関連記事

分類Dev

UTCからJavaへの標準ISO8601形式の入力文字列を解析する方法は?

分類Dev

JodaTime DateTime、ISO8601 GMT日付形式

分類Dev

Pandas read_csv not recognizing ISO8601 as datetime dtype

分類Dev

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

分類Dev

正規表現およびISO8601形式のDateTime

分類Dev

ISO8601のDateTime + TimeZoneおよびマイクロ秒なし

分類Dev

DateTimeをISO8601に変換します

分類Dev

How to store DateTime at UTC format with Java 8?

分類Dev

java.time.ZonedDateTime.parseおよびiso8601?

分類Dev

MySQLフィールドDATETIMEはISO8601を切り捨てます

分類Dev

ISO8601期間からDateTimeオブジェクトを取得します

分類Dev

ISO8601フィールドからPythonのDateTimeフィールド

分類Dev

ISO8601日付文字列をUTCタイムゾーンで日付まで解析します

分類Dev

JavaでISO8601の週番号から日付を計算する方法

分類Dev

AndroidでISO8601文字列をJava日付に解析する方法

分類Dev

Postgresql jsonb vs datetime

分類Dev

ISO8601形式でDateTimeオブジェクトを解析および生成する方法

分類Dev

ISO8601準拠の文字列をperlのDateTimeオブジェクトに解析する方法は?

分類Dev

OrientDBでISO8601準拠の日付をDateTime形式として設定する方法はありますか?

分類Dev

タイムゾーン付きのISO8601日付をJuliaのDateTimeに変換します

分類Dev

正規表現およびISO8601形式のDateTime2016-10-18T00:00:00Zのみ

分類Dev

ISO8601ローカルユーザーのタイムゾーンに表示されるDateTime文字列

分類Dev

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

分類Dev

ensure that utc datetime is persisted

分類Dev

ISO 8601DateTime表現

分類Dev

すべてのISO8601DateTime形式を解析しますJava1.8

分類Dev

どのようにJavaでISO 8601形式にUTCの日時を変換するには?

分類Dev

JavaでISO8601日付をUNIXタイムスタンプに変換する

分類Dev

JavaでISO8601タイムスタンプ文字列をエポック秒に変換する

Related 関連記事

  1. 1

    UTCからJavaへの標準ISO8601形式の入力文字列を解析する方法は?

  2. 2

    JodaTime DateTime、ISO8601 GMT日付形式

  3. 3

    Pandas read_csv not recognizing ISO8601 as datetime dtype

  4. 4

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

  5. 5

    正規表現およびISO8601形式のDateTime

  6. 6

    ISO8601のDateTime + TimeZoneおよびマイクロ秒なし

  7. 7

    DateTimeをISO8601に変換します

  8. 8

    How to store DateTime at UTC format with Java 8?

  9. 9

    java.time.ZonedDateTime.parseおよびiso8601?

  10. 10

    MySQLフィールドDATETIMEはISO8601を切り捨てます

  11. 11

    ISO8601期間からDateTimeオブジェクトを取得します

  12. 12

    ISO8601フィールドからPythonのDateTimeフィールド

  13. 13

    ISO8601日付文字列をUTCタイムゾーンで日付まで解析します

  14. 14

    JavaでISO8601の週番号から日付を計算する方法

  15. 15

    AndroidでISO8601文字列をJava日付に解析する方法

  16. 16

    Postgresql jsonb vs datetime

  17. 17

    ISO8601形式でDateTimeオブジェクトを解析および生成する方法

  18. 18

    ISO8601準拠の文字列をperlのDateTimeオブジェクトに解析する方法は?

  19. 19

    OrientDBでISO8601準拠の日付をDateTime形式として設定する方法はありますか?

  20. 20

    タイムゾーン付きのISO8601日付をJuliaのDateTimeに変換します

  21. 21

    正規表現およびISO8601形式のDateTime2016-10-18T00:00:00Zのみ

  22. 22

    ISO8601ローカルユーザーのタイムゾーンに表示されるDateTime文字列

  23. 23

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

  24. 24

    ensure that utc datetime is persisted

  25. 25

    ISO 8601DateTime表現

  26. 26

    すべてのISO8601DateTime形式を解析しますJava1.8

  27. 27

    どのようにJavaでISO 8601形式にUTCの日時を変換するには?

  28. 28

    JavaでISO8601日付をUNIXタイムスタンプに変換する

  29. 29

    JavaでISO8601タイムスタンプ文字列をエポック秒に変換する

ホットタグ

アーカイブ