Open In App

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:



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.

Syntax:



static ChronoPeriod between(ChronoLocalDate startDateInclusive,
                            ChronoLocalDate endDateExclusive)

Parameters:

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:




// Java code to show the chronoPeriod
// between the given start and end date
  
import java.time.*;
import java.time.chrono.*;
  
public class ChronoPeriodClass {
  
    // Function to calculate chronoPeriod between
    // start and end date
    static void
    calculateChronoPeriod(ChronoLocalDate startDate,
                          ChronoLocalDate endDate)
    {
        ChronoPeriod chronoPeriod
            = ChronoPeriod.between(startDate, endDate);
        System.out.println("ChronoPeriod between start and end "
                           + "date is : " + chronoPeriod);
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        // Start date
        ChronoLocalDate startDate
            = LocalDate.parse("2017-02-13");
  
        // End date
        ChronoLocalDate endDate
            = LocalDate.parse("2018-08-20");
  
        calculateChronoPeriod(startDate, endDate);
    }
}

Output:
ChronoPeriod between start and end date is : P1Y6M7D

Reference: https://docs.oracle.com/javase/9/docs/api/java/time/chrono/ChronoPeriod.html#between-java.time.chrono.ChronoLocalDate-java.time.chrono.ChronoLocalDate-


Article Tags :