Java jackson configuration date

431 views Asked by At

I am using jackson configurator to serialize - deserialize date.

I am using this

SerializationConfig serConfig = mapper.getSerializationConfig();
serConfig.setDateFormat(new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z"));

DeserializationConfig deserializationConfig = mapper.getDeserializationConfig();
deserializationConfig.setDateFormat(new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z"));

Now if I pass 10/10/2013 02:30:00 EST it allows me to do so but 10/10/2013 02:30:00 gives me error.

How to make configurator parse both?

1

There are 1 answers

0
Basil Bourque On

You should not be passing a date-time without a time zone. To do so is nonsense, without meaning. Like saving a local phone number without an area code.

Jackson saves its dates, as it should, in UTC time zone. That means no time zone at all. So the date-time you pass must have a time zone so that Jackson may adjust the value to UTC. I don't actually know or use Jackson, but that's what it's doc says.

If the code passing the date-time knows or can deduce or infer the time zone, it should do so and append a time zone.

If you are passing a date-time that is already in UTC, then add a time zone of "Z", for Zulu.

Beware: Using 3-letter time zone codes is not a good practice. They are not standardized and frequently have duplicates. The EST value in your example means at least 3 time zones around the world: US, Australia, and Brazil. Use name of time zone instead.

Tip: If you do any work with dates in Java, get the third-party Joda-Time library rather than use the java.util.Date/Calendar classes.