加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java-使用格式自定义反序列化日期

发布时间:2020-12-14 19:30:48 所属栏目:Java 来源:网络整理
导读:[last_modified])] with root cause java.time.format.DateTimeParseException: Text 2018-06-06T13:19:53+00:00 could not be parsed,unparsed text found at index 19 入站格式为2018-06-06T13:19:53 00:00 这是一种奇怪的格式. 我尝试了以下方法: publ

[“last_modified”])] with root cause
java.time.format.DateTimeParseException: Text ‘2018-06-06T13:19:53+00:00’ could not be parsed,unparsed text found at index 19

入站格式为2018-06-06T13:19:53 00:00
这是一种奇怪的格式.

我尝试了以下方法:

public class XYZ {  
    @DateTimeFormat(pattern = "yyyy-MM-ddTHH:mm:ss+00:00",iso = ISO.DATE_TIME)
    private LocalDateTime lastModified;
}  
最佳答案
没有什么可以阻止您创建自己的解串器.一个非常幼稚的示例如下:

public class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {

    private static final String PATTERN = "yyyy-MM-dd'T'HH:mm:ss+00:00";

    private final DateTimeFormatter formatter;

    public LocalDateTimeDeserializer() {
        this.formatter = DateTimeFormatter.ofPattern(PATTERN);
    }

    @Override
    public LocalDateTime deserialize(JsonParser p,DeserializationContext ctxt) throws IOException {
        return LocalDateTime.parse(p.getText(),formatter);
    }
}

您需要注意的唯一一件事是,您需要通过在其周围添加单引号来转义“ T”.

使用解串器后,您可以像这样简单地注释字段:

@JsonDeserialize(using = LocalDateTimeDeserializer.class)
private LocalDateTime dateTime;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读