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

时间:2015-05-19 08:08:14

标签: java datetime

我将日期作为"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"
},

有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:-1)

自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);