ObjectMapper converting to wrong time

I am giving date as "scoreTimestamp": "2015-04-15T10:00:00.000Z",

And want to store this json as a string in the database using the method

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

      

I want the date to be like this: "scoreTimestamp": "2015-04-15 10:00" where as objectMapper converts this to:

  "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"
},

      

Can anyone help me?

+3


source to share


1 answer


Since Jackson v2.0, you can use the @JsonFormat annotation directly on the fields:

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

      



or make it default

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

      

-1


source







All Articles