title: How do I (un)marshall a Date as a time-stamp with jackson
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>Here is an excerpt of my POM file:</p>
  
  <pre><code>&lt;dependency&gt;
      &lt;groupId&gt;javax.ws.rs&lt;/groupId&gt;
      &lt;artifactId&gt;javax.ws.rs-api&lt;/artifactId&gt;
      &lt;version&gt;2.0.1&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.glassfish.jersey.media&lt;/groupId&gt;
      &lt;artifactId&gt;jersey-media-json-jackson&lt;/artifactId&gt;
      &lt;version&gt;2.15&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>Sample model class:</p>
  
  <pre><code>public class PersonJSON {
      private Date birthDate;
  }
  </code></pre>
  
  <p>Expected output (assuming birth date is 01 Feb 2015 00:00:00 UTC-0):</p>
  
  <pre><code>{"birthDate":1422748800000}
  </code></pre>
  
  <p>Current output:</p>
  
  <pre><code>{"birthDate":"2015-02-01"}
  </code></pre>
  
  <p>Is it possible to fix this (ideally using an annotation)?</p>
  
  <p><strong>Solution</strong></p>
  
  <p>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.</p>
  
  <p>If you do want to alter the format the approaches listed by peeskillet is correct assuming you have a valid Java.util.Date object.</p>