ChronoPeriod between() method in Java with Examples
The between() method of ChronoPeriod interface in Java is used to obtain a chronoPeriod consisting of the number of years, months, and days between two given dates (including start date and excluding end date).
This chronoPeriod is obtained as follows:
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
- Remove complete months.
- Now, calculate the remaining number of days.
- Then, adjust to ensure that both have the same sign.
- Now, split the number of months into years and months based on a 12 month year.
- Consider a month, if the end day-of-month is greater than or equal to the start day-of-month (Eg.: from 2017-05-12 to 2018-07-18 is one year, two months and six days).
Note: ChronoPeriod obtained from above formula can be a negative, if the end is before the start. The negative sign will be the same in each of year, month and day.
static ChronoPeriod between(ChronoLocalDate startDateInclusive, ChronoLocalDate endDateExclusive)
- startDateInclusive – The start date is inclusive and must not be null.
- endDateExclusive – The end date is exclusive and must not be null.
Return Value: The between() function of chronoPeriod returns the chronoPeriod between the given start and end date.
Below is the implementation of the above function:
ChronoPeriod between start and end date is : P1Y6M7D