SharpLu
SharpLu

Reputation: 1214

Java 8 calculate months between two dates

NOTE THIS IS NOT A DUPLICATE OF EITHER OF THE FOLLOWING


I have two dates:

Its 91 days duration between the above two dates, I expected my code to return 3 months duration, but the below methods only returned 2 months. Does anyone have a better suggestion? Or do you guys think this is a bug in Java 8? 91 days the duration only return 2 months.

Thank you very much for the help.

Method 1:

Period diff = Period.between(LocalDate.parse("2016-08-31"),
    LocalDate.parse("2016-11-30"));

Method 2:

long daysBetween = ChronoUnit.MONTHS.between(LocalDate.parse("2016-08-31"),
    LocalDate.parse("2016-11-30"));

Method 3:

I tried to use Joda library instead of Java 8 APIs, it works. it loos will return 3, It looks like Java duration months calculation also used days value. But in my case, i cannot use the Joda at my project. So still looking for other solutions.

    LocalDate dateBefore= LocalDate.parse("2016-08-31");
    LocalDate dateAfter = LocalDate.parse("2016-11-30");
    int months = Months.monthsBetween(dateBefore, dateAfter).getMonths();
    System.out.println(months);

Upvotes: 60

Views: 135303

Answers (10)

DHINESH
DHINESH

Reputation: 1

Sorted out, seems to be tricky

 Period period = Period.between(startDate, endDate);
    if ((endDate.getMonth() == Month.FEBRUARY && period.getDays() >= Month.FEBRUARY.length(Year.isLeap(endDate.getYear())) &&
            endDate.isEqual(getLastDayofTheMonth(endDate))) ||
            (startDate.isEqual(getLastDayofTheMonth(startDate)) &&
                    endDate.isEqual(getLastDayofTheMonth(endDate)) &&
                    endDate.getDayOfMonth() == ODD_MONTH_DAYS)) {
        period = period.plusMonths(1);
    }

    return period.getYears() * 12 + period.getMonths();

Upvotes: 0

DavidJezek
DavidJezek

Reputation: 111

The Java API response is mathematically accurate according to the calendar. But you need a similar mechanism, such as rounding decimals, to get the number of months between dates that matches the human perception of the approximate number of months between two dates.

Period period = Period.between(LocalDate.parse("2016-08-31"), LocalDate.parse("2016-11-30"));
long months = period.toTotalMonths();
if (period.getDays() >= 15) {
    months++;
}

Upvotes: 0

Flips
Flips

Reputation: 170

This example checks to see if the second date is the end of that month. If it is the end of that month and if the first date of month is greater than the second month date it will know it will need to add 1

LocalDate date1 = LocalDate.parse("2016-08-31");
LocalDate date2 = LocalDate.parse("2016-11-30");
    
long monthsBetween = ChronoUnit.MONTHS.between(
            date1,
            date2);
    
if (date1.isBefore(date2) 
        && date2.getDayOfMonth() == date2.lengthOfMonth() 
        && date1.getDayOfMonth() > date2.getDayOfMonth()) {
    monthsBetween += 1;
}

Upvotes: 2

Yandi Ongkojoyo
Yandi Ongkojoyo

Reputation: 141

//Backward compatible with older Java

public static int monthsBetween(Date d1, Date d2){
    if(d2==null || d1==null){
        return -1;//Error
    }
    Calendar m_calendar=Calendar.getInstance();
    m_calendar.setTime(d1);
    int nMonth1=12*m_calendar.get(Calendar.YEAR)+m_calendar.get(Calendar.MONTH);
    m_calendar.setTime(d2);
    int nMonth2=12*m_calendar.get(Calendar.YEAR)+m_calendar.get(Calendar.MONTH);
    return java.lang.Math.abs(nMonth2-nMonth1);
}

Upvotes: 14

NashBird99
NashBird99

Reputation: 303

You have to be careful, never use LocalDateTime to calculate months between two dates the result is weird and incorrect, always use LocalDate !

here's is some code to prove the above:

package stack.time;

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

public class TestMonthsDateTime {
    public static void main(String[] args) {
        /**------------------Date Time----------------------------*/
        LocalDateTime t1 = LocalDateTime.now();
        LocalDateTime t2 = LocalDateTime.now().minusMonths(3);
        long dateTimeDiff = ChronoUnit.MONTHS.between(t2, t1);  
        System.out.println("diff dateTime : " + dateTimeDiff); // diff dateTime : 2
        /**-------------------------Date----------------------------*/
        LocalDate t3 = LocalDate.now();
        LocalDate t4 = LocalDate.now().minusMonths(3);
        long dateDiff = ChronoUnit.MONTHS.between(t4, t3);
        System.out.println("diff date : "  +  dateDiff); // diff date : 3
    }
}

My 2%

Upvotes: 3

AxelH
AxelH

Reputation: 14572

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

Upvotes: 72

Zon
Zon

Reputation: 19880

Since Java8:

ChronoUnit.MONTHS.between(startDate, endDate);

Upvotes: 18

abhi
abhi

Reputation: 4792

In case you want stick to java.time.Period API

As per java.time.Period documentation

Period between(LocalDate startDateInclusive, LocalDate endDateExclusive)

where

@param startDateInclusive  the start date, inclusive, not null
@param endDateExclusive  the end date, exclusive, not null

So it is better to adjust your implementation to make your end date inclusive and get your desired result

Period diff = Period.between(LocalDate.parse("2016-08-31"),
                LocalDate.parse("2016-11-30").plusDays(1));
System.out.println("Months : " + diff.getMonths());
//Output -> Months : 3

Upvotes: 2

SharpLu
SharpLu

Reputation: 1214

After the short investigation, still not totally fix my question, But I used a dirty solution to avoid return the incorrect duration. At least, we can get the reasonable duration months.

private static long durationMonths(LocalDate dateBefore, LocalDate dateAfter) {
        System.out.println(dateBefore+"   "+dateAfter);
        if (dateBefore.getDayOfMonth() > 28) {
            dateBefore = dateBefore.minusDays(5);
        } else if (dateAfter.getDayOfMonth() > 28) {
            dateAfter = dateAfter.minusDays(5);
        }
        return ChronoUnit.MONTHS.between(dateBefore, dateAfter);
    }

Upvotes: 1

Jacob G.
Jacob G.

Reputation: 29680

The documentation of Period#between states the following:

The start date is included, but the end date is not.

Furthermore:

A month is considered if the end day-of-month is greater than or equal to the start day-of-month.

Your end day-of-month 30 is not greater than or equal to your start day-of-month 31, so a third month is not considered.

Note the parameter names:

public static Period between​(LocalDate startDateInclusive, LocalDate endDateExclusive)

To return 3 months, you can increment the endDateExclusive by a single day.

Upvotes: 7

Related Questions