Period between(LocalDate startDateInclusive,
LocalDate endDateExclusive)
creates a Period consisting of the number of years, months, and days between two dates.
between
has the following syntax.
public static Period between(LocalDate startDateInclusive, LocalDate endDateExclusive)
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 www . j av a2s. c om 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.