I have this date string coming in through JSON: "29-OCT-21 12.00.00.000000000 AM UTC". I want to save it as a ZonedDateTime data type.
I have the property set in the model as such:
@JsonProperty("createdTs") @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MMM-yy hh.mm.ss.SSSSSSSSS a z", with = JsonFormat.Feature.ACCEPT_CASE_INSENSITIVE_PROPERTIES) private ZonedDateTime createdTs; I am getting an error:
org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.time.ZonedDateTime` from String "29-OCT-21 12.00.00.000000000 AM UTC": Failed to deserialize java.time.ZonedDateTime: (java.time.format.DateTimeParseException) Text '29-OCT-21 12.00.00.000000000 AM UTC' could not be parsed I can't figure out what is wrong. The pattern works just fine in the formatter in test cases, like this:
DateTimeFormatter formatter = new DateTimeFormatterBuilder().parseCaseInsensitive().appendPattern("dd-MMM-yy hh.mm.ss.SSSSSSSSS a z").toFormatter( Locale.getDefault()); locationPayload.setcreatedTs(ZonedDateTime.parse("29-OCT-21 12.00.00.000000000 AM UTC", formatter));
JavaTimeModule?2021-10-29T00:00:00Z.Zmeans UTC, so if that's the only time zone you need, then you are set. You also wouldn't need aZonedDateTimebut could instead use anInstantor anOffsetDateTime.ZonedDateTimeis appropriate, and you would specify the time zone as its IANA identifier, such asAmerica/New_York- either following the string (separated by space or in square brackets without a space), or as a separate field.