[英]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
您問:
這個日期時間字符串的 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.