This example will demonstrate how to find the number of years between two dates using Java 8 date time api and Joda time. We will set the start date to December 25, 2004 and the end date to January 1, 2006. Then we will find the number of years between the start date and end date which equates to 1 year.
Java 8 Date and Time API
This snippet will find the difference between two dates in years using java 8 Duration.between and ChronoUnit.YEARS. Both approaches will calculate the amount of time between two temporal objects in the form of years.
Joda Time
Using joda, this snippet will calculate number of years between two dates using Years.yearsBetween.