0
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 ?

Mustahsan
  • 3,852
  • 1
  • 18
  • 34
Parameswar
  • 1,951
  • 9
  • 34
  • 57

2 Answers2

3

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)

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
  • if the number of days is 30 and the month just contains 30 days, then it does not make sense right ? – Parameswar Oct 18 '19 at 07:19
  • 1
    From the javadoc: This method adds the specified amount to the days field incrementing themonth and year fields as necessary to ensure the result remains valid – sfiss Oct 18 '19 at 07:20
  • 1
    `plusDays` simply adds one day to the period, nothing more. This may cause the month and year to be incremented accordingly. – MC Emperor Oct 18 '19 at 07:22
3

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)

sfiss
  • 2,119
  • 13
  • 19