title: ZonedDateTime persistance to SQL Database
body: <p>Good day,<br />
  I create web application service, which would be used across the world. <br />
  Therefore I need to store datetime values in UTC and represent them in clocks on the wall time to the end user.<br />
  After reading Stack overflow, I understood, that I should: <br /></p>
  
  <ol>
  <li>use timestamp as a column type in DB (currently MariaDB 10.1.20)</li>
  <li>use ZonedDateTime in Java (I use java8)</li>
  </ol>
  
  <p>The problem arose while converting between these values.<br />
  As I use JDBC, I have to do the following conversion: <br /></p>
  
  <pre><code>java.sql.Timestamp &lt;-&gt; java.time.ZonedDateTime 
  </code></pre>
  
  <p>My code:<br /></p>
  
  <pre><code>// Get current zonedDateTime
  ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneOffset.UTC);
  
  // Convert zonedDateTime to java.sql.Timestamp before saving to DB
  Timestamp = Timestamp.from(zonedDateTime.toInstant());
  
  // Get zonedDateTime from resultSet
  Timestamp timestamp = (Timestamp) resultSet.getObject("created");
  ZonedDateTime zonedDateTime = 
      ZonedDateTime.ofInstant(ts.toInstant(), ZoneOffset.UTC))
  </code></pre>
  
  <p>When I use:</p>
  
  <pre><code>zonedDateTimeBeforeSave.isEqual(zonedDateTimeAfterSave);
  </code></pre>
  
  <p>It returns me false (I need to compare them in overridden equal method of my domain model)<br />
  Below is print out of both:<br /></p>
  
  <blockquote>
    <p>zonedDateTimeBeforeSave: 2017-01-24T20:18:17.720Z<br />
    zonedDateTimeAfterSave:  2017-01-24T20:18:17Z</p>
  </blockquote>
  
  <p>Questions:</p>
  
  <ol>
  <li>Am I right in my choice. Perhaps, I should use another column or java type...</li>
  <li>Am I right in doing conversions. Maybe there is another better way</li>
  </ol>
  
  <p>Thank you</p>
  
  <ol>
  <li>Edit:
  After Matt Johnson's help I understood that the problem is in the fact, that when I save datetime to DB, It doesn't save fractions, though It should. FYI column type is timestamp(6).</li>
  <li>Edit:
  Now I use java.time.Instant instead of ZonedDateTime</li>
  </ol>