title: Java 8 java.time: Adding TemporalUnit in Instant vs LocalDateTime
body: <p>I'm playing around with the new java.time package in Java 8. I have a legacy database that gives me <code>java.util.Date</code>, which I convert to <code>Instant</code>.</p>
  
  <p>What I am trying to do is add a period of time that is based off of another database flag. I could be adding days, weeks, months, or years. I don't want to have to care what I am adding, and I would like to be able to add more options in the future.</p>
  
  <p>My first thought was <code>Instant.plus()</code>, but that gives me an <code>UnsupportedTemporalTypeException</code> for values greater than a day. Instant apparently does not support operations on large units of time. Fine, whatever, <code>LocalDateTime</code> does. </p>
  
  <p>So that gives me this code:</p>
  
  <pre><code>private Date adjustDate(Date myDate, TemporalUnit unit){
      Instant instant = myDate.toInstant();
      LocalDateTime dateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
      dateTime = dateTime.plus(1, unit);
      Instant updatedInstant = dateTime.atZone(ZoneId.systemDefault()).toInstant();
      return new Date(dueInstant.toEpochMilli());
  }
  </code></pre>
  
  <p>Now, this is my first time using the new time API, so I may have missed something here. But it seems clunky to me that I have to go:</p>
  
  <pre><code>Date --&gt; Instant --&gt; LocalDateTime --&gt; do stuff--&gt; Instant --&gt; Date.
  </code></pre>
  
  <p>Even if I did not have to use the Date part, I would still think it was a bit awkward.  So my question is this, am I doing this completely wrong and what is the best way to do this? </p>
  
  <hr>
  
  <p><strong>Edit</strong>: Expanding on the discussion in the comments.</p>
  
  <p>I think I have a better idea now about how LocalDateTime and Instant are playing with java.util.Date and java.sql.Timestamp. Thanks everyone.</p>
  
  <p>Now, a more practical consideration.  Let's say a user sends me a date from wherever they are in the world, arbitrary time zone.  They send me <code>2014-04-16T13:00:00</code> which I can parse into a LocalDateTime.  I then convert this directly to a java.sql.Timestamp and persist in my database.</p>
  
  <p>Now, without doing anything else, I pull my java.sql.timestamp from my database, convert to <code>LocalDateTime</code> using <code>timestamp.toLocalDateTime()</code>. All good. Then I return this value to my user using the ISO_DATE_TIME formatting. The result is <code>2014-04-16T09:00:00</code>.</p>
  
  <p>I assume this difference is because of some type of implicit conversion to/from UTC. I think my default time zone may be getting applied to the value (EDT, UTC-4) which would explain why the number is off by 4 hours.</p>
  
  <p>New question(s). Where is the implicit conversion from local time to UTC happening here?  What is the better way to preserve time zones. Should I not be going directly from Local time as a string (2014-04-16T13:00:00) to <code>LocalDateTime</code>? Should I be expecting a time zone from the user input?</p>