title: Subtleties between Java Period and Duration
body: <p>I'm not sure I'm getting the subtleties between Java <code>Period</code> and <code>Duration</code>.</p>
  
  <p>When I read Oracle's <a href="https://docs.oracle.com/javase/tutorial/datetime/iso/period.html" rel="noreferrer">explanation</a>, it says that I can find out how many days since a birthday like this (using the example dates they used):</p>
  
  <pre><code>LocalDate today = LocalDate.now();
  LocalDate birthday = LocalDate.of(1960, Month.JANUARY, 1);
  Period birthdayPeriod = Period.between(birthday, today);
  int daysOld = birthdayPeriod.getDays();
  </code></pre>
  
  <p>But as even they point out, this doesn't take into account the time zone you were born in and the time zone you are in now. But this is a computer and we can be precise, right? So would I use a <code>Duration</code>?</p>
  
  <pre><code>ZoneId bornIn = ZoneId.of("America/New_York");
  ZonedDateTime born = ZonedDateTime.of(1960, Month.JANUARY.getValue(), 1, 2, 34, 56, 0, bornIn);
  ZonedDateTime now = ZonedDateTime.now();
  Duration duration = Duration.between(born, now);
  long daysPassed = duration.toDays();
  </code></pre>
  
  <p>Now the actual times are accurate, but if I understand this correctly, the days might not correctly represent calendar days, e.g. with DST and such.</p>
  
  <p>So what am I do to to get a precise answer based upon my time zone? The only thing I can think of is to go back to using <code>LocalDate</code>, but normalize the time zones first from the <code>ZonedDateTime</code> values, and then use a <code>Duration</code>.</p>
  
  <pre><code>ZoneId bornIn = ZoneId.of("America/New_York");
  ZonedDateTime born = ZonedDateTime.of(1960, Month.JANUARY.getValue(), 1, 2, 34, 56, 0, bornIn);
  ZonedDateTime now = ZonedDateTime.now();
  ZonedDateTime nowNormalized=now.withZoneSameInstant(born.getZone());
  Period preciseBirthdayPeriod = Period.between(born.toLocalDate(), nowNormalized.toLocalDate());
  int preciseDaysOld = preciseBirthdayPeriod.getDays();
  </code></pre>
  
  <p>But that seems really complicated just to get a precise answer.</p>