Hello:
How can I use annotations to deserialize Greenwich Time into LocalTime?
Here is Demo class:
public class Demo implements Serializable {
private LocalDateTime time;
// setter getter ...
}
Here is Demo JSON:
{"time" : "2019-05-08T07:45:44.519Z"}
The time I expect to get is 2019-05-08T15:45:44.519, but what I get is: 2019-05-08T07:45:44.519
Now I'm using custom deserialized classes to solve the problem:
public class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {
private static final String ZULU = "Z";
@Override
public LocalDateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
String text = jsonParser.getText();
if (text.endsWith(ZULU)) {
return LocalDateTime.ofInstant(Instant.parse(text), ZoneId.systemDefault());
} else {
return LocalDateTime.parse(text, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
}
}
}
public class Demo implements Serializable {
@JsonDeserialize(using = CustomLocalDateTimeDeserializer.class)
private LocalDateTime time;
// setter getter ...
}
Is there any other way?
I would appreciate your help.