Java Calculate Number of Months Between two LocalDate

Tags: Java LocalDate ChronoUnit Java 8

In this Java core tutorial, we learn how to calculate the number of months between two LocalDate objects using the java.time.temporal.ChronoUnit class in Java programming language.

How to get number of months between LocalDate in Java

In Java, we can use the ChronoUnit.MONTHS.between() method to calculate the number of months between two LocalDate values as the example Java code below.

MonthsBetweenLocalDateExample.java

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

public class MonthsBetweenLocalDateExample {
    public static void main(String... args) {
        LocalDate start = LocalDate.of(2010, 1, 15);
        LocalDate end = LocalDate.of(2022, 12, 30);

        // Calculate Number of Months Between two LocalDate
        long numberOfMonths = ChronoUnit.MONTHS.between(start, end);

        System.out.println(String.format("Number of months between %s and %s is %d", start, end, numberOfMonths));
    }
}
The output as below.
Number of months between 2010-01-15 and 2022-12-30 is 155

Happy Coding 😊

Java Calculate Number of Decades Between two LocalDate

Java Calculate Number of Years Between two LocalDate

Java Calculate Number of Weeks Between two LocalDate

Java Calculate Number of Days Between two LocalDate