gpt4 book ai didi

java - ObjectMapper 正在转换为错误的日期时间

转载 作者:行者123 更新时间:2023-12-02 08:31:07 27 4
gpt4 key购买 nike

我将日期传递为 "scoreTimestamp": "2015-04-15T10:00:00.000Z",并希望将此 json 作为字符串保存在数据库中,因此使用方法

 ObjectMapper objMapper = new ObjectMapper();
String ratingData = objMapper.writeValueAsString(scoreTimestamp);

我希望日期为:“scoreTimestamp”:“2015-04-15 10:00”,其中objectMapper将其转换为:

  "scoreTimestamp" : {
"year" : 2015,
"dayOfMonth" : 15,
"dayOfWeek" : 3,
"era" : 1,
"dayOfYear" : 105,
"monthOfYear" : 4,
"weekyear" : 2015,
"yearOfEra" : 2015,
"yearOfCentury" : 15,
"weekOfWeekyear" : 16,
"centuryOfEra" : 20,
"millisOfSecond" : 0,
"millisOfDay" : 36000000,
"secondOfMinute" : 0,
"secondOfDay" : 36000,
"minuteOfHour" : 0,
"minuteOfDay" : 600,
"hourOfDay" : 10,
"zone" : {
"fixed" : true,
"id" : "UTC"
},

有人可以帮我吗?

最佳答案

从 Jackson v2.0 开始,您可以直接在字段上使用 @JsonFormat 注解:

@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", timezone="GMT")
private Date scoreTimestamp;

或将其设置为默认

DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm a z");
objMapper.setDateFormat(df);

关于java - ObjectMapper 正在转换为错误的日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30320049/

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