Since you don't care about the days in your case. You only want the number of month between two dates, use the documentation of the period to adapt the dates, it used the days as explain by Jacob. Simply set the days of both instance to the same value (the first day of the month)

Period diff = Period.between(
            LocalDate.parse("2016-08-31").withDayOfMonth(1),
            LocalDate.parse("2016-11-30").withDayOfMonth(1));
System.out.println(diff); //P3M

Same with the other solution :

long monthsBetween = ChronoUnit.MONTHS.between(
        LocalDate.parse("2016-08-31").withDayOfMonth(1),
        LocalDate.parse("2016-11-30").withDayOfMonth(1));
System.out.println(monthsBetween); //3

Edit from @Olivier Grégoire comment:

Instead of using a LocalDate and set the day to the first of the month, we can use YearMonth that doesn't use the unit of days.

long monthsBetween = ChronoUnit.MONTHS.between(
     YearMonth.from(LocalDate.parse("2016-08-31")), 
     YearMonth.from(LocalDate.parse("2016-11-30"))
)
System.out.println(monthsBetween); //3
Answer from AxelH on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java dates › difference between two dates in java
Difference Between Two Dates in Java | Baeldung
May 8, 2025 - In Java 8, the Time API introduced two new classes: Duration and Period. If we want to calculate the difference between two date-times in a time-based (hour, minutes, or seconds) amount of time, we can use the Duration class:
🌐
How to do in Java
howtodoinjava.com › home › java date time › java – difference between two dates
Java - Difference Between Two Dates - HowToDoInJava
February 18, 2022 - Java 8 made the first attempt to upgrade this date/time API. The ChronoUnit instance represents a standard set of date periods units. We can use its different types of instances to find the difference in specific time measures. LocalDate dateOfBirth = LocalDate.of(1980, Month.JULY, 4); LocalDate currentDate = LocalDate.now(); long diffInDays = ChronoUnit.DAYS.between(dateOfBirth, currentDate); long diffInMonths = ChronoUnit.MONTHS.between(dateOfBirth, currentDate); long diffInYears = ChronoUnit.YEARS.between(dateOfBirth, currentDate);
🌐
w3resource
w3resource.com › java-exercises › datetime › java-datetime-exercise-30.php
Java - Difference between two dates (year, months, days)
Write a Java program to compute and display the precise period between two dates using the Period class. Write a Java program to compare two dates and output the difference in a "Y years, M months, D days" format.
🌐
Medium
medium.com › @AlexanderObregon › javas-period-between-method-explained-c32f4cd996c6
Java’s Period.between() Method Explained | Medium
January 1, 2025 - Difference: 2 years, 6 months, 16 days. ... The method calculates the gap between 2022-06-01 and 2024-12-17, breaking it into years, months, and days. This is useful when comparing two fixed dates or measuring durations between past events.
Top answer
1 of 16
622

Simple diff (without lib)

/**
 * Get a diff between two dates
 * @param date1 the oldest date
 * @param date2 the newest date
 * @param timeUnit the unit in which you want the diff
 * @return the diff value, in the provided unit
 */
public static long getDateDiff(Date date1, Date date2, TimeUnit timeUnit) {
    long diffInMillies = date2.getTime() - date1.getTime();
    return timeUnit.convert(diffInMillies,TimeUnit.MILLISECONDS);
}

And then you can call:

getDateDiff(date1,date2,TimeUnit.MINUTES);

to get the diff of the 2 dates in minutes unit.

TimeUnit is java.util.concurrent.TimeUnit, a standard Java enum going from nanos to days.


Human readable diff (without lib)

public static Map<TimeUnit,Long> computeDiff(Date date1, Date date2) {

    long diffInMillies = date2.getTime() - date1.getTime();

    //create the list
    List<TimeUnit> units = new ArrayList<TimeUnit>(EnumSet.allOf(TimeUnit.class));
    Collections.reverse(units);

    //create the result map of TimeUnit and difference
    Map<TimeUnit,Long> result = new LinkedHashMap<TimeUnit,Long>();
    long milliesRest = diffInMillies;

    for ( TimeUnit unit : units ) {
        
        //calculate difference in millisecond 
        long diff = unit.convert(milliesRest,TimeUnit.MILLISECONDS);
        long diffInMilliesForUnit = unit.toMillis(diff);
        milliesRest = milliesRest - diffInMilliesForUnit;

        //put the result in the map
        result.put(unit,diff);
    }

    return result;
}

http://ideone.com/5dXeu6

The output is something like Map:{DAYS=1, HOURS=3, MINUTES=46, SECONDS=40, MILLISECONDS=0, MICROSECONDS=0, NANOSECONDS=0}, with the units ordered.

You just have to convert that map to a user-friendly string.


Warning

The above code snippets compute a simple diff between 2 instants. It can cause problems during a daylight saving switch, like explained in this post. This means if you compute the diff between dates with no time you may have a missing day/hour.

In my opinion the date diff is kind of subjective, especially on days. You may:

  • count the number of 24h elapsed time: day+1 - day = 1 day = 24h

  • count the number of elapsed time, taking care of daylight savings: day+1 - day = 1 = 24h (but using midnight time and daylight savings it could be 0 day and 23h)

  • count the number of day switches, which means day+1 1pm - day 11am = 1 day, even if the elapsed time is just 2h (or 1h if there is a daylight saving :p)

My answer is valid if your definition of date diff on days match the 1st case

With JodaTime

If you are using JodaTime you can get the diff for 2 instants (millies backed ReadableInstant) dates with:

Interval interval = new Interval(oldInstant, new Instant());

But you can also get the diff for Local dates/times:

// returns 4 because of the leap year of 366 days
new Period(LocalDate.now(), LocalDate.now().plusDays(365*5), PeriodType.years()).getYears() 

// this time it returns 5
new Period(LocalDate.now(), LocalDate.now().plusDays(365*5+1), PeriodType.years()).getYears() 

// And you can also use these static methods
Years.yearsBetween(LocalDate.now(), LocalDate.now().plusDays(365*5)).getYears()
2 of 16
214

The JDK Date API is horribly broken unfortunately. I recommend using Joda Time library.

Joda Time has a concept of time Interval:

Interval interval = new Interval(oldTime, new Instant());

EDIT: By the way, Joda has two concepts: Interval for representing an interval of time between two time instants (represent time between 8am and 10am), and a Duration that represents a length of time without the actual time boundaries (e.g. represent two hours!)

If you only care about time comparisions, most Date implementations (including the JDK one) implements Comparable interface which allows you to use the Comparable.compareTo()

🌐
w3resource
w3resource.com › java-exercises › datetime › java-datetime-exercise-19.php
Java - Get year and months between two dates
1 month ago - Write a Java program to determine the number of complete months between two user-provided dates. Write a Java program to compute the gap in years and remaining months between two dates.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › find-the-duration-of-difference-between-two-dates-in-java
Find the duration of difference between two dates in Java - GeeksforGeeks
July 15, 2025 - Method 3: Use Period class in Java to find the difference between two days. The Period.between() method is used to calculate the difference between two dates in years, months, and days.
🌐
Medium
medium.com › @AlexanderObregon › calculating-the-difference-between-two-dates-in-java-fcd6ebb28994
Calculating the Difference Between Two Dates in Java
September 13, 2025 - Learn how Java calculates the difference between two dates with LocalDate, Period, and ChronoUnit, covering epoch days, leap years, and calendar mechanics.
🌐
Blogger
javarevisited.blogspot.com › 2016 › 10 › how-to-get-number-of-months-and-years-between-two-dates-in-java.html
3 ways to get number of months and year between two dates in Java? Examples
Here is the complete code to find the number of months and years between two dates in Java using java.util.Calendar class. It uses GregorianCalendar, which we all use in our day-to-day life. You must remember that month in the Calendar starts from zero i.e.
🌐
Baeldung
baeldung.com › home › java › java dates › calculate months between two dates in java
Calculate Months Between Two Dates in Java | Baeldung
January 30, 2024 - In this tutorial, we’ll delve into details of how to use the legacy Date API, Date Time API, and Joda-Time library to calculate month intervals between two dates in Java.
🌐
Java67
java67.com › 2020 › 01 › how-to-find-difference-between-two-dates-in-java8.html
How to find difference between two dates in Java 8? Example Tutorial | Java67
Learn Java and Programming through articles, code examples, and tutorials for developers of all levels. ... One of the most common programming task while working with date and time objects are calculating the difference between dates and finding a number of days, months, or years between two dates.
🌐
Pega
support.pega.com › question › difference-between-two-dates
Difference Between the Two Dates | Support Center
February 2, 2024 - java.time.LocalDate sDate = java.time.LocalDate.parse(startDate, java.time.format.DateTimeFormatter.ofPattern("yyyy/MM/dd")); java.time.LocalDate eDate = java.time.LocalDate.parse(endDate, java.time.format.DateTimeFormatter.ofPattern("yyyy/MM/dd")); java.time.Period period = java.time.Period.between(sDate, eDate); int years = period.getYears(); int months = period.getMonths(); int days = period.getDays(); String YearsString = (years > 1) ? "years" : "year"; String monthsString = (months > 1) ?
🌐
Level Up Lunch
leveluplunch.com › java › examples › number-of-months-between-two-dates
Months between two dates | Level Up Lunch
February 4, 2014 - @Test public void months_between_two_dates_in_java_with_java8 () { LocalDate startDate = LocalDate.of(2004, Month.JANUARY, 1); LocalDate endDate = LocalDate.of(2005, Month.JANUARY, 1); long monthsInYear2 = ChronoUnit.MONTHS.between(startDate, endDate); assertEquals(12, monthsInYear2); }
🌐
TutorialsPoint
tutorialspoint.com › how-to-get-days-months-and-years-between-two-java-localdate
How to get days, months and years between two Java LocalDate?
July 30, 2019 - HR Interview Questions · Computer ... = LocalDate.of(2019, 4, 29); Now, get the difference between two dates with Period class between() method: Period p = Period.between(date1, date2); Now, get the years, month and days: p.getYears() p.getMonths() p.getDays() import ...
🌐
Coderanch
coderanch.com › t › 683575 › java › Calculate-number-months-dates
Calculate number of months between two dates (Java in General forum at Coderanch)
August 21, 2017 - So if I were born on Jan 2, 1975, how would you calculate my age in months? There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors ... Without thinking about very clever ways (if there are such), have look at the LocalDate class. Task supposed to be quite simple. ... I came up with a solution. Here it is: ... Don't use Calendar. A dreadful class to use. Use LocalDate←link. You can read about the new date classes in the Java™ Tutorials.
🌐
Netjstech
netjstech.com › 2017 › 11 › difference-between-two-dates-java-program.html
Difference Between Two Dates in Java | Tech Tutorials
August 24, 2022 - Java 8 onward you can use new date and time API classes Period and Duration to find difference between two dates. Period class is used to model amount of time in terms of years, months and days.