Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a function on java 8 API to count days between two dates (both inclusive)?

The signature of ChronoUnit.DAYS.between method is:

public long between(Temporal temporal1Inclusive, Temporal temporal2Exclusive)

So the last date is not included as it relates the example:

LocalDate from = LocalDate.now();
LocalDate to = from.plusDays(1);

ChronoUnit.DAYS.between(from, to); // Result is 1

Is there another function to get 2 days in that expression?

Otherwise I only see that I could do that as the following:

LocalDate from = LocalDate.now();
LocalDate to = from.plusDays(1);

ChronoUnit.DAYS.between(from, to.plusDays(1)); // Result is 2
like image 802
Joe Avatar asked Dec 19 '17 13:12

Joe


People also ask

How do I get the number of days between two dates in Java?

To calculate the days between two dates we can use the DAYS. between() method of java. time. temporal.


1 Answers

Actually NO, but you can use the following code:

ChronoUnit.DAYS.between(from, to) + 1;

+1 adds 1 day to the difference between those two local dates so we can say that the last date is included.

like image 88
Alex Mamo Avatar answered Sep 23 '22 01:09

Alex Mamo