This example will demonstrate how to find the number of months between two dates using Java 8 date time api and Joda time. We will set the start date to January 1, 2004 and the end date to January 1, 2005. Then we will find the number of months between the start date and end date which equates to 12 months.
Java 8 Date and Time API
This snippet will find the difference between two dates in months using java 8 Duration.between and ChronoUnit.MONTHS.between. Both approaches will calculate the amount of time between two temporal objects in the form of months.
Joda Time
Using joda, this snippet will calculate number of months between two dates using Months.monthsBetween.