title: Calculating number of years between two Date or Calendar objects, considering leap years
body: <p>So in a Java project I'm working on we are required to use <em>only</em> Date and Calendar objects to represent dates. A method I'm writing requires that a date be at least certain number of years in the past, so I need to be able to accurately calculate the number of years between the present and the given Date or Calendar. I have managed to accurately calculate the number of days between using this implementation: </p>
  
  <pre><code>public static long daysSince(Date pastDate) {
      long millisecondsSince = new Date().getTime() - pastDate.getTime();
      return TimeUnit.DAYS.convert(millisecondsSince, TimeUnit.MILLISECONDS);
  }
  </code></pre>
  
  <p>However I am now struggling to find a way to accurately calculate the number of years between these dates whilst taking into consideration leap years etc. Obviously dividing the result of the above method by 365 or 365.25 does not quite work. I am aware of the joda time package and java.time however we explicitly need to work with Date and Calendar objects. Anyone got any idea how this can be done, preferably as quickly and elegantly as possible? Thank you</p>
  
  <p>EDIT: Seemed to find functioning solution, see below</p>