title: java.time: Compare two Instants - get the number of hours, minutes, seconds, years, months between the two
body: <p>I tried this code:</p>
  
  <pre><code>public class TimePassed {
      private long seconds;
      private long minutes;
      private long hours;
      private long days;
      private long years;
      ...
  
      public TimePassed(double unixSeconds)  {
          Instant now = Instant.now();
          Instant ago = Instant.ofEpochSecond((long) unixSeconds);
  
          this.seconds = ChronoUnit.SECONDS.between(
              ago.atZone(ZoneId.systemDefault()),
              now.atZone(ZoneId.systemDefault()));  //6100
          this.minutes = ChronoUnit.MINUTES.between(
              ago.atZone(ZoneId.systemDefault()),
              now.atZone(ZoneId.systemDefault()));  //101
          this.hours = ChronoUnit.HOURS.between(
              ago.atZone(ZoneId.systemDefault()),
              now.atZone(ZoneId.systemDefault()));  //1
          this.days = ChronoUnit.DAYS.between(
              ago.atZone(ZoneId.systemDefault()),
              now.atZone(ZoneId.systemDefault()));  //0
          this.years = ChronoUnit.YEARS.between(
              ago.atZone(ZoneId.systemDefault()),
              now.atZone(ZoneId.systemDefault()));  //0
      }
  }
  </code></pre>
  
  <p>However then the <code>TimePassed</code> object would have <code>seconds = 6100</code> and <code>minutes = 101</code> and <code>hours = 1</code>, while I want it to be <code>hours = 1</code>, <code>minutes = 41, seconds = 40</code>, so that <code>60*60 + 41*60 + 40 = 6100</code>. Is it possible to do with <code>java.time</code> package? Because as of now I can only either get passed seconds, passed minutes or passed hours, etc. And neither would account for the other.</p>