Java 8 calculate months between two dates

We Are Going To Discuss About Java 8 calculate months between two dates. So lets Start this Java Article.

Java 8 calculate months between two dates

  1. Java 8 calculate months between two dates

    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)

  2. Java 8 calculate months between two dates

    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)

Solution 1

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

Original Author AxelH Of This Content

Solution 2

//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);
}

Original Author Yandi Ongkojoyo Of This Content

Solution 3

Since Java8:

ChronoUnit.MONTHS.between(startDate, endDate);

Original Author Zon Of This Content

Solution 4

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.

Original Author Jacob G. Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment