Period difference = Period.between(01.01.2018,31.01.2018)
difference.getMonths()
gives zero and difference.getDays()
gives 30 days, anyway to include the last day ?
Period difference = Period.between(01.01.2018,31.01.2018)
difference.getMonths()
gives zero and difference.getDays()
gives 30 days, anyway to include the last day ?
The answer is utterly simple: just call plusDays(1)
on the resulting period.
Period.between
does not include the end day on purpose. In order to include the last day, you must manually add one day yourself.
This pattern of including the start point and excluding the end point is quite common. For example, String.substring(int startIndex, int endIndex)
also follows this pattern, as well as IntStream.range(int startInclusive, int endExclusive)
It is in the documentation (end date exclusive):
The start date is included, but the end date is not.
You could simply add one day to your enddate via endDate.plusDays(1)