Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 calculate months between two dates

Tags:

java

java-8

NOTE THIS IS NOT A DUPLICATE OF EITHER OF THE FOLLOWING

  • Calculating the difference between two Java date instances
  • calculate months between two dates in java [duplicate]

I have two dates:

  • Start date: "2016-08-31"
  • End date: "2016-11-30"

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); 
like image 648
SharpLu Avatar asked Feb 23 '18 14:02

SharpLu


People also ask

How do you calculate months in Java?

Once you have the LocalDate, you can use Months. monthsBetween() and Years. yearsBetween() method to calcualte the number of months and years between two dates in Java. LocalDate jamesBirthDay = new LocalDate(1955, 5, 19); LocalDate now = new LocalDate(2015, 7, 30); int monthsBetween = Months.

How can we get duration between two dates or time in Java 8?

Using JodaTime, you can get the difference between two dates in Java using the following code: Days d = Days. daysBetween(startDate, endDate). getDays();

Which of the following codes will you use to add a period between two dates in Java 8?

Method 1: Period diff = Period. between(LocalDate. parse("2016-08-31"), LocalDate.


1 Answers

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 
like image 190
AxelH Avatar answered Sep 21 '22 17:09

AxelH