Period between(LocalDate startDateInclusive, LocalDate endDateExclusive) example

Description

Period between(LocalDate startDateInclusive, LocalDate endDateExclusive) creates a Period consisting of the number of years, months, and days between two dates.

Syntax

between has the following syntax.


public static Period between(LocalDate startDateInclusive,   LocalDate endDateExclusive)

Example

The following example shows how to use between.


import java.time.LocalDate;
import java.time.Month;
import java.time.Period;
import java.time.temporal.ChronoUnit;
/*from   w  w w.  ja va 2s  . co  m*/
public class Main {
  public static void main(String[] args) {
    Period p = Period.between(LocalDate.of(2009, Month.JANUARY, 21),LocalDate.of(2019, Month.JANUARY, 21));
    
    System.out.println(p.get(ChronoUnit.DAYS));


    LocalDate today = LocalDate.now();
    LocalDate birthday = LocalDate.of(1960, Month.JANUARY, 1);

    LocalDate nextBDay = birthday.withYear(today.getYear());

    nextBDay = nextBDay.plusYears(1);

    p = Period.between(today, nextBDay);
    long p2 = ChronoUnit.DAYS.between(today, nextBDay);
    System.out.println(p.getMonths() + " months");
    System.out.println(p.getDays() + " days");
  }
}

The code above generates the following result.





















Home »
  Java Date Time »
    java.time Reference »




Clock
DayOfWeek
Duration
Instant
LocalDate
LocalDateTime
LocalTime
Month
MonthDay
OffsetDateTime
OffsetTime
Period
Year
YearMonth
ZonedDateTime
ZoneId
ZoneOffset