Reputation: 183
What is the JsonFormat Pattern for this Datetime String?
"2021-02-16Z09:38:35"
It has been defined as a STRING (ISO DATETIME FORMAT).
I have defined it as -
@JsonProperty("lastDT")
@JsonFormat(pattern = "yyyy-MM-dd'Z'HH:mm:ss", shape = JsonFormat.Shape.STRING)
private ZonedDateTime lastDT;
I keep receiving a JSON Parsing error
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
Upvotes: 1
Views: 1568
Reputation: 339043
You asked:
What is the JsonFormat Pattern for this Datetime String?
"2021-02-16Z09:38:35"
Looks like somebody tried for a standard ISO 8601 format but failed.
In ISO 8601, there should be a T
between the date portion and time-of-day portion of a string.
Correct format would be 2021-02-16T09:38:35
rather than 2021-02-16Z09:38:35
with a T
, not Z
.
I suggest you educate the publisher of your data feed about correct usage of ISO 8601.
LocalDateTime
, not ZonedDateTime
A string like 2021-02-16T09:38:35
indicates a date and a time but lacks any indication of time zone or offset-from-UTC. So you should change your code to use LocalDateTime
rather than ZonedDateTime
.
String inputCorrected = "2021-02-16Z09:38:35".replace( "Z" , "T" ) ;
LocalDateTime ldt = LocalDateTime.parse( inputCorrected ) ;
A Z
is used in ISO 8601 to indicate an offset of zero hours-minutes-seconds from UTC. The letter is pronounced “Zulu” per aviation/military tradition.
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
Upvotes: 2