Using Java 8 time I am simply trying to figure out the number of days represented in a time range. Consider the following:
LocalDate start = LocalDate.of(2016, Month.MARCH, 28);
LocalDate end = LocalDate.of(2016, Month.MARCH, 31);
Period period = Period.between(start, end);
The number of days in period is 3 which represents the number of days between the 2 dates, inclusive of start and exclusive of end. What I want is the number of days represented by the 2 dates which is actually 4 (March 28, March 29, March 30, March 31).
I know I can just add 1 to the number of days returned from Period.between() but I guess I was surprised that I couldn't find another call to return exactly what I want. Am I missing something or is adding 1 the only solution?
the signature of the method is
and the method is not oerloaded, you have no choice other than add 1 to the given result...
There doesn't seem to be an inclusive end date method in either
Period
orLocalDate
so it seems that the only thing to do is something like:or
(
Period.between
just callsLocalDate.until
)I belive your day counting differs from java's. Period.between according to documentation: http://docs.oracle.com/javase/8/docs/api/java/time/Period.html#between-java.time.LocalDate-java.time.LocalDate- "The start date is included, but the end date is not." Having that in mind - yes, adding 1 is the only solution.
tl;dr
Always define your spans of time by the Half-Open approach where:
When you want the four days of
March 28, March 29, March 30, March 31
, make the beginning March 28 and the ending April 1. Run through those dates starting at the first (March 28th) while going up to, but not including, the last (April 1st).Half-Open
You may be missing an appreciation for the usefulness of the Half-Open approach in defining spans of time.
Generally, the best practice for defining spans of time is the Half-Open approach. In Half-Open, the beginning is inclusive while the ending is exclusive.
This approach solves the problem of dealing with fractional seconds. Intuitively, many programmers will try to find the last possible moment as the ending of a span of time. But that last moment involves an infinitely divisible last second. You might think, "Well, just go to three decimal place for milliseconds, 12:59.59.999 for the end of noon lunch break, as that is all the resolution I will ever need, and that is the resolution of the legacy java.util.Date class.”. But then you would fail to find matches in your database like Postgres that store date-time values with a resolution of microseconds, 12:59:59.999999. So you decide to use six decimal places of fraction, x.999999. But the start experiencing mismatches with date-time values in the java.time classes, and you learn the offer a resolution of nanoseconds for nine digits of fractional second, x.999999999. You can disembark this carousel of frustrating bugs by using the Half-Open approach where the ending runs up to, but does not include, the next whole second.
I believe you will find consistent use of the Half-Open approach throughout your date-time handling code (whether fractional seconds may be involved or not) will:
Knowing all your spans of time carry the same definition eliminates ambiguity.
Examples:
LocalDate
Rather than one adding
1
to get a total of days, define your span of time as Half-Open: beginning-is-inclusive, ending-is-exclusive. If you are trying to represent the four dates of March 28, 29, 30, and 31, then I suggest you define a span of time from March 28 to April 1.Period
The java.time classes wisely use the Half-Open approach. So the
Period.between
method treats the ending as exclusive, as noted in the Question. I suggest you go-with-the-flow here rather than fight it. Search Stack Overflow for many more examples of how well this approach works.ChronoUnit
If you want a total number of days, such as 45 for a month and a half, use the
ChronoUnit
enum, an implementation ofTemporalUnit
. See this Question for discussion.Again, the java.time classes use the Half-Open approach. So
Live code
See this example code run live at IdeOne.com.