3

I am passing date as "scoreTimestamp": "2015-04-15T10:00:00.000Z", And want to save this json as string in database so using method

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

I want date to be as: "scoreTimestamp":"2015-04-15 10:00", where as objectMapper is converting 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 somebody help me please?

  • Please see the answer(s) mentioned in the article https://stackoverflow.com/questions/41876037/jackson-date-format-for-offsetdatetime-in-spring-boot – kuti Jun 28 '21 at 13:04

1 Answers1

-1

Since Jackson v2.0, you can use @JsonFormat annotation directly on 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);
Milan Baran
  • 4,133
  • 2
  • 32
  • 49
  • Its not working..its still same "scoreTimestamp": { "year": 2015, "dayOfMonth": 15, "dayOfWeek": 3, "era": 1, "dayOfYear": 105, "millisOfSecond": 0, "millisOfDay": 36000000, "secondOfMinute": 0, "secondOfDay": 36000, "minuteOfHour": 0, "minuteOfDay": 600, "hourOfDay": 10, "monthOfYear": 4, "weekyear": 2015, "yearOfEra": 2015, "weekOfWeekyear": 16, "yearOfCentury": 15, ......etc – Gourav khanna May 19 '15 at 09:45
  • Not working for me either. I have the `DateTime` objects in a `Map` inside the object to serialise. – Gabriel Petrovay Oct 19 '16 at 09:25