我需要在一段代码中将下面两个foramt反序列化为java.time.Instant
2020-04-23T10:51:24.238+01:00和2019-11-11T15:44:10.201Z
在第一种情况下,我遇到了很大的错误
嵌套异常是org.springframework.http.converter.HttpMessageNotReadableException: JSON分析错误:无法反序列化字符串“2020-04-23T10:51:24.238+01:00”中的java.time.Instant类型的值: com.fasterxml.jackson.databind.exc.InvalidFormatException:无法反序列化字符串“2020-04-23T10:51:24.238+01:00”中的java.time.Instant类型的值:无法反序列化java.time.Instant:(java.time.format.DateTimeParseException) Text '2020-04-23T10:51:24.238+01:00‘无法在索引23处分析
有什么解决方案吗?另外,有没有办法将java.util.Date反序列化为java.time.Instant注意:这是一个API响应,我不能在反序列化的类中使用Date,即消费者
发布于 2020-05-11 16:08:20
还有什么方法可以将java.util.Date反序列化为java.time.Instant吗
了解你的API:
final java.util.Date date = new Date();
final java.time.Instant instant = date.toInstant();发布于 2020-05-13 00:31:10
String dateString = "2020-04-23T10:51:24.238+01:00";
Instant deserializedInstant = DateTimeFormatter.ISO_OFFSET_DATE_TIME
.parse(dateString, Instant::from);
System.out.println(deserializedInstant);输出为:
2020-04-23T09:51:24.238Z
它也适用于你的其他字符串:
String dateString = "2019-11-11T15:44:10.201Z";2019-11-11T15:44:10.201Z
您的JSON库可能还有更好的解决方案。如果不是这样,您可能可以将上述内容包装在自定义JSON反序列化程序中。
https://stackoverflow.com/questions/61724859
复制相似问题