title: How to deserialise JSON datetime with nanosecond precision using gson
body: <p>I'm trying to deserialise a JSON object using gson, but having issues when it comes to dates. The date is deserialised from the JSON object, but because the value in the JSON object is in nanoseconds, the value I get is slightly off of the expected value. </p>
  
  <p>See the following code</p>
  
  <p><strong>JSONClass</strong></p>
  
  <pre><code>public class JSONClass {
      private Date timestamp;
  
      public Date getTimestamp() {
          return timestamp;
      }
  
      public void setTimestamp(Date timestamp) {
          this.timestamp = timestamp;
      }
  }
  </code></pre>
  
  <p><strong>Main</strong></p>
  
  <pre><code>public class GsonTestApplication {
      public static void main(String[] args) {
          final Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss.SSS").create();
          final String responseJSON = "{ \"timestamp\":\"2017-11-09 11:07:20.079364+00\" }";
          final JSONClass foo = gson.fromJson(responseJSON, new TypeToken&lt;JSONClass&gt;(){}.getType());
          System.out.println(foo.getTimestamp().toString());
      }
  }
  </code></pre>
  
  <p>The output of the application is </p>
  
  <pre><code>Thu Nov 09 11:08:39 GMT 2017
  </code></pre>
  
  <p>When I expect it to be</p>
  
  <pre><code>Thu Nov 09 11:07:20 GMT 2017
  </code></pre>
  
  <p>I don't care about the nanosecond precision, so I'm happy for this to be truncated, but as I don't have control over the JSON format, I'm not sure the best way to do this. </p>
  
  <p><strong>How can I get gson to deserialise the date correctly?</strong></p>