Date format Mapping to JSON Jackson

Kevin Rave picture Kevin Rave · Sep 17, 2012 · Viewed 430k times · Source

I have a Date format coming from API like this:

"start_time": "2015-10-1 3:00 PM GMT+1:00"

Which is YYYY-DD-MM HH:MM am/pm GMT timestamp. I am mapping this value to a Date variable in POJO. Obviously, its showing conversion error.

I would like to know 2 things:

  1. What is the formatting I need to use to carry out conversion with Jackson? Is Date a good field type for this?
  2. In general, is there a way to process the variables before they get mapped to Object members by Jackson? Something like, changing the format, calculations, etc.

Answer

Olivier Lecrivain picture Olivier Lecrivain · Nov 24, 2014

Since Jackson v2.0, you can use @JsonFormat annotation directly on Object members;

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm a z")
private Date date;