Need to identify number of months between two dates using java 8 but not getting proper number of months. Please find below program
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy", Locale.ENGLISH);
YearMonth startDate = YearMonth.parse("12/31/2020", formatter);
YearMonth endDate = YearMonth.parse("12/01/2021", formatter);
System.out.println(startDate.plusYears(1) + " :: " + endDate + " :: " + startDate.plusYears(1).isBefore(endDate));
long monthsBetween = ChronoUnit.MONTHS.between(startDate, endDate);
System.out.println(monthsBetween);
}
Output ::
2021-12 :: 2021-12 :: false
12
So from above program we are getting 12 months between this 2 dates "12/31/2020" and "12/01/2021"
But actually no of months between above dates is 13 so how we are getting 12 instead of 13?
May be I am wrong but can someone please explain
Note : date format is MM/dd/yyyy