簡體   English   中英

這個 DateTime 的 JSON 格式模式是什么?

[英]What is the JSON format pattern for this DateTime?

這個日期時間字符串的 JsonFormat 模式是什么?

"2021-02-16Z09:38:35"

它已被定義為字符串(ISO 日期時間格式)。

我將其定義為 -

@JsonProperty("lastDT")
@JsonFormat(pattern = "yyyy-MM-dd'Z'HH:mm:ss", shape = JsonFormat.Shape.STRING)
private ZonedDateTime lastDT;

我不斷收到 JSON 解析錯誤

Failed to deserialize java.time.ZonedDateTime: (java.time.DateTimeException) Unable to obtain 
ZonedDateTime from TemporalAccessor: {},ISO resolved to 2021-02-16T09:38:35 of type java.time.format.Parsed

ISO 8601 格式,拙劣

您問:

這個日期時間字符串的 JsonFormat 模式是什么?

"2021-02-16Z09:38:35"

看起來有人嘗試使用標准ISO 8601格式但失敗了。

在 ISO 8601 中,字符串的日期部分和時間部分之間應該有一個T

正確的格式是2021-02-16T09:38:35而不是2021-02-16Z09:38:35帶有T ,而不是Z

我建議您對數據源的發布者進行有關正確使用 ISO 8601 的教育。

使用LocalDateTime ,而不是ZonedDateTime

2021-02-16T09:38:35這樣的字符串表示日期和時間,但沒有任何時區指示或與 UTC 的偏移量。 因此,您應該更改代碼以使用LocalDateTime而不是ZonedDateTime

String inputCorrected = "2021-02-16Z09:38:35".replace( "Z" , "T" ) ;
LocalDateTime ldt = LocalDateTime.parse( inputCorrected ) ;

祖魯時間

A Z在 ISO 8601 中用於表示與 UTC 的零時分秒偏移量。 根據航空/軍事傳統,這封信發音為“Zulu”。

Instant instant = Instant.now() ;    // Represent a moment as seen in UTC, an offset of zero hours-minutes-seconds from UTC.
String output = instant.toString() ;

2021-02-16T09:38:35Z

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM