I need to manipulate with dates according my task. To be precise I need to check that we have month difference between 2 dates as integer. 2 dates- input arguments. Result - boolean: true
if month count is exact(without tail) false
otherwise.
For example:
diff_months between 31.01.2018
and 28.02.2018
should be 1 month and it is possible combination.
30.01.2018
and 28.02.2018
: 1 month without tail, so ok
29.01.2018
and 28.02.2018
: 1 month without tail, so ok
28.01.2018
and 28.02.2018
: 1 month without tail, so ok
27.01.2018
and 28.02.2018
: 0 months
lets take another arguments:
28.02.2018
and 28.03.2018
: 1 month without tail, so ok
28.02.2018
and 29.03.2018
: 1 month +1 day, so fail
28.02.2018
and 27.03.2018
: 0 month with tail, so fail
I found that java date api has special API for date calculation but from my point of view it works contradictory:
LocalDate.of(2018, 1, 31).until(LocalDate.of(2018, 2, 28), ChronoUnit.MONTHS); // 0
LocalDate.of(2018, 1, 31).plusMonths(1); // but 2018-02-28
The second result is expected but firs - not. It looks really weird.
Can you please advise method for month count calculation between two dates which works in contract with the LocalDate#plusMonths
method ?