Varun Achar Varun Achar - 1 year ago 166
Java Question

How to deserialize JS date using Jackson?

I'm getting a date string from ExtJS in the format:


when i try to deserialize this date, it changes the timezone to Indian Standard Time (adds +5:30 to the time) . This is how i'm deserializing the date:

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

Doing this also doesn't change the timezone. I still get the date in IST:

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

How do I deserialize the date in the way in which it is coming without the hassles of Timezone?

Answer Source

I found a work around but with this I'll need to annotate each date's setter throughout the project. Is there a way in which I can specify the format while creating the ObjectMapper?

Here's what I did:

public class CustomJsonDateDeserializer extends JsonDeserializer<Date>
    public Date deserialize(JsonParser jsonparser,
            DeserializationContext deserializationcontext) throws IOException, JsonProcessingException {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        String date = jsonparser.getText();
        try {
            return format.parse(date);
        } catch (ParseException e) {
            throw new RuntimeException(e);



And annotated each Date field's setter method with this:

@JsonDeserialize(using = CustomJsonDateDeserializer.class)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download