gpt4 book ai didi

java - 如何用 Jackson 序列化 LocalDateTime?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:06:36 25 4
gpt4 key购买 nike

我得到了以下代码:

    ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
String now = new ObjectMapper().writeValueAsString(new SomeClass(LocalDateTime.now()));
System.out.println(now);

我明白了:

{"time":{"hour":20,"minute":49,"second":42,"nano":99000000,"dayOfYear":19,"dayOfWeek":"THURSDAY","month":"JANUARY","dayOfMonth":19,"year":2017,"monthValue":1,"chronology":{"id":"ISO","calendarType":"iso8601"}}}

我要实现的是ISO8601中的字符串

2017-01-19T18:36:51Z

最佳答案

这可能是由于您的代码有误。您正在使用新的未配置的映射器实例,这是修复方法:

 ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
String now = mapper.writeValueAsString(new SomeClass(LocalDateTime.now()));
System.out.println(now);

关于java - 如何用 Jackson 序列化 LocalDateTime?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41749539/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com