Javascript and Java Date JSON serialization Javascript and Java Date JSON serialization spring spring

Javascript and Java Date JSON serialization


I prefer to stick to javascripts ISO 8601 date format,when parsing it correctly it'll will automatically handle timezone differences.

In java you can parse a javascript Stringified JSON date as follows:

String iso8601Date = "2013-08-13T14:15:00.000Z";SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");Date parsedDate = formatter.parse(iso8601Date);

When turning it back into strings, you'll have something like this

//"2013-08-13T16:15:00.000+02:00"String formattedDate = formatter.format(parsedDate);

For parsing JSON I use FlexJson, which you can configure like this.

//Serializing to JSONDateTransformer dateTransformer = new DateTransformer("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");new JSONSerializer().transform(dateTransformer, Date.class).serialize(object);//deserialize From JSON (replace object by java class)JSONDeserializer<..Object..>().use(Date.class, dateTransformer).deserialize(json);


I would suggest passing the date/times around using their seconds since epoch notation, more specifically the number of seconds since the Unix Epoch (1 Jan 1970 00:00 GMT). If you're not familiar with this, there is an example converter here: http://www.epochconverter.com/

This has a few advantages:

  • It refers to the same moment in time independently of the time zone. This helps storing the time independently of time zone errors (although they would have to be input correctly in the first place, of course).
  • It's the only non-deprecated constructor (except constructor without params) in java.util.Date, see (getTime() too). (Note that this uses milliseconds.)
  • JavaScript can build date from it quite easily (e.g. new Date(1000 * 1326894706)). (Note that this uses milliseconds.)
  • If it matters, it's always going to be a bit smaller (in terms of data size in its JSON serialization) than any of "yyyy-MM-dd HH:mm:ss".
  • If you want the time zone to be rendered and associated with this number, you could always add an extra field to your representation. { "datetime": 1326894706, "tz": "GMT" } is still shorter than { "datetime": "18 Jan 2012 13:51:46 GMT" }.

Considering it's easy to get Date instances from this in Java and JavaScript, you can then use a DateFormatter to convert it to/from text in Java. For JavaScript, using a library such as Date Format will help you render it as appropriate on the page (for example with something like new Date(val * 1000).format("yyyy-mm-dd HH:MM")).


If you use the Spring Framework you can use the @JsonSerialize and @JsonDeserialize annotations. It will parse correctly your javascript Date.

Example:

public class product {    @NotNull    private Long id;        @NotNull    private String name;    @JsonSerialize(using = CustomLocalDateSerializer.class)    @JsonDeserialize(using = ISO8601LocalDateDeserializer.class)    private LocalDate date;    // getter and setter...}