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.