I'm having trouble (un)marshalling java.util.Date objects into timestamps. Ideally the timestamps should be in a UTC-0 format and not the server's local time zone. Although I can work around that pretty easily if I need to.
NB: I am aware that here are several similar topics on stack overflow but everyone I have come across is either outdated (with respect to the API's being used) or are related to serializing Date objects to strings.
Here is an excerpt of my POM file:
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.15</version>
</dependency>
Sample model class:
public class PersonJSON {
private Date birthDate;
}
Expected output (assuming birth date is 01 Feb 2015 00:00:00 UTC-0):
{"birthDate":1422748800000}
Current output:
{"birthDate":"2015-02-01"}
Is it possible to fix this (ideally using an annotation)?
Solution
As it turns out the error was caused by an implicit conversion from Java.sql.Date to Java.util.Date. The conversion throws no exceptions and the sql.Date extends util.Date so logically it should work, but it doesn't. The solution was to extract the timestamp from the Java.sql.Date and use it as input to the Java.util.Date constructor.
If you do want to alter the format the approaches listed by peeskillet is correct assuming you have a valid Java.util.Date object.