2

How to count the number of days between two LocalDate's in java? I tried like this

LocalDate startDate = LocalDate.parse("2016-11-02");
LocalDate endDate = LocalDate.parse("2016-11-04");

long days = startDate.until(endDate, ChronoUnit.DAYS);

//and

long days= ChronoUnit.DAYS.between(startDate, endDate);

//and

long days= Duration.between(startDate.atStartOfDay(), endDate.atStartOfDay()).toDays();

All above are giving days between two dates, but how to count inclusive of both dates. As per above steps, getting 2 days as no. of days, but required 3 days.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
sreedhar
  • 296
  • 1
  • 5
  • 15
  • It is better to use java.time.Duration – Bilal BBB Dec 11 '17 at 12:38
  • Possible duplicate of [Calculating the difference between two Java date instances](https://stackoverflow.com/questions/1555262/calculating-the-difference-between-two-java-date-instances) – Ole V.V. Dec 11 '17 at 12:54
  • 1
    Including the two given dates — isn’t that a matter of adding 1?? `Duration` has a `plusDays` method that will do this for you. – Ole V.V. Dec 11 '17 at 12:56
  • why to hardcode +1 .why not the the exact count from predefined methods. – sreedhar Dec 11 '17 at 12:57
  • 2
    I understand your question, but in this case I see no harm in hardcoding +1. The unofficial standard in date and time intervals is to use half-open, that is, including the start and excluding the end. So you will have to subtract one to get the exclusive count (only the day between the two) and conversely add one when you want the count to be inclusive. – Ole V.V. Dec 11 '17 at 12:59

3 Answers3

4

The javadoc is clear that the end date is not included in the calculation. So you can simply add 1:

long days = ChronoUnit.DAYS.between(startDate, endDate) + 1;
assylias
  • 321,522
  • 82
  • 660
  • 783
3

tl;dr

Beginning being inclusive, and ending being exclusive in a span of time is the norm.

If you absolutely cannot follow that approach, add one.

days = days + 1 ;

Half-Open

A common and wise way to define a span of time is the Half-Open approach. The beginning is inclusive while the ending is exclusive.

Sometimes, but not always, we use this approach intuitively in everyday life. For example, a classroom’s lunch period is said to be noon to 1 PM which means all the students are to be returned to class and ready before the class strikes 13:00. Class is said to run 1 PM to 2 PM. So the spans can abut one another without overlapping, and without the tricky task of determining the last moment of an infinitely divisible last second of last minute of that hour.

I suggest following this approach in all your date-time coding will make your code easier to read, easier to debug, and less error-prone by reducing the cognitive overload of resolving the inclusive-exclusive ambiguity.

The java.time classes use the Half-Open approach throughout.

long days = ChronoUnit.DAYS.between( start , stop ) ;  // Half-open

If you code must return an ending-inclusive result, just add one to the java.time results.

long daysClosed = ( days + 1 ) ;

Ideally you would use Half-Open consistently in your code and in your user interface, while educating your users as to the issue of ambiguity. I have seen countless mistakes made by businesspeople wrongly assuming that date ranges were open (), closed [], or half-open [), and even occasionally the opposite half closed (] while the author meant another.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
2

If you want literal 24 hour days, you can use the Duration class instead:

long days = Duration.between(startDate.atStartOfDay(), endDate.atStartOfDay()).toDays() + 1;

For more information, please see this document regarding Java SE 8 Date and Time.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • 1
    I think you need to add +1 to make both dates inclusive. between is defined as: `between(Temporal startInclusive, Temporal endExclusive)` – Eritrean Dec 11 '17 at 12:52