Thursday, October 9, 2025
HomeLanguagesJavaPeriod between() method in Java with Examples

Period between() method in Java with Examples

The between() method of Period class in Java is used to obtain a period consisting of the number of years, months, and days between two given dates (including start date and excluding end date).

This period is obtained as follows:

  • 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: Period 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:

public static Period between(LocalDate startDateInclusive,
                             LocalDate endDateExclusive)

Parameters:

  • 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 period returns the period between the given start and end date.

Below is the implementation of above function:




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


Output:

Period between start and end date is : P1Y6M7D

Reference: https://docs.oracle.com/javase/8/docs/api/java/time/Period.html#between-java.time.LocalDate-java.time.LocalDate-

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32346 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6715 POSTS0 COMMENTS
Nicole Veronica
11877 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11940 POSTS0 COMMENTS
Shaida Kate Naidoo
6835 POSTS0 COMMENTS
Ted Musemwa
7094 POSTS0 COMMENTS
Thapelo Manthata
6789 POSTS0 COMMENTS
Umr Jansen
6791 POSTS0 COMMENTS