Skip to content
This repository has been archived by the owner on Mar 3, 2019. It is now read-only.

Inconsistency between default serialization of java.util.Date and OffsetDateTime #22

Open
KonstantinYegupov opened this issue Oct 12, 2015 · 2 comments

Comments

@KonstantinYegupov
Copy link

java.util.Date is serialized to epoch milliseconds by default; OffsetDateTime - to epoch seconds. That makes migration to Java 8 date classes exceedingly complicated.

@joschi
Copy link
Member

joschi commented Oct 12, 2015

@KonstantinYegupov I'm not sure what you're hinting at. Could you please elaborate on the problem you have?

@KonstantinYegupov
Copy link
Author

Assume you have a data type with a "date" field. You want to serve instances of the type via REST API.

If you use legacy java.util.Date class for the "date" field, in the JSON generated by Dropwizard you will see the "date" field represented as a long integer with the value of "milliseconds since Unix epoch", e.g. 1444664056000. That's not very human-readable, but kind of acceptable. Epoch millis are pretty standard, many JS frontends are able to deal with that.

Assume someone told you java.util.Date is terrible and it's a good policy to never ever use it (which happens to be true). You listen and convert all the date types in your application to OffsetDateTime introduced in Java 8.

But now your "date" values are returned by in JSON as SECONDS since Unix epoch, e.g. 1444664056. This causes your frontend to interpret them as dates in early 1970. Sorrow and lamentation.

What's worse, it's not exactly obvious how to configure the date conversion.

The workaround I'm using at the moment is:

environment.getObjectMapper().setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'"));

which probably should have been the default behavior.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants