title: Java 8 -- How do I calculate (milli)seconds from now using xsd:duration
body: <p>I feel like this question has been asked in one way or another, but I'm still not confident of my result.</p>
  
  <p>I have an xsd:duration which will give me a desired expiration described in years, months, days, and seconds. I can collect the integer values of these parts with, for example, <code>duration.getYears()</code> or <code>duration.getMonths()</code>.</p>
  
  <p>Because my chosen db is Cassandra, I want to exploit the TTL option, which will automatically expire an inserted row after a specified number of seconds.</p>
  
  <p>The critical part is getting from xsd:duration to an integer/long value of seconds which respects the Gregorian calendar (where 1 month from now is not simply 30.41 days, but 31).</p>
  
  <p>At the moment, I'm using the following code:</p>
  
  <pre><code>LocalDateTime then = LocalDateTime.now().plusYears(duration.getYears()).plusMonths(duration.getMonths()).plusDays(duration.getDays()).plusHours(duration.getHours()).plusMinutes(duration.getMinutes()).plusSeconds(duration.getSeconds());
  long ttlMillis = then.toInstant(ZoneOffset.UTC).toEpochMilli() - Instant.now().toEpochMilli();
  </code></pre>
  
  <p>Is there a quicker/cleaner way to do this?</p>
  
  <p>I'm also not sure if I should worry about large durations... My particular use cases wouldn't call for anything larger that 2 years.</p>