Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Period — a calendar amount between dates

java.time.Period represents an amount in years, months, and days. Period.between calculates that amount between two LocalDate values, rather than elapsed time in seconds.

Version: Java SE 21; repository runtime, production runtime not attested

Syntax

Period.between(LocalDate startDateInclusive, LocalDate endDateExclusive)

java.time.Period is an immutable, thread-safe value representing years, months, and days in the ISO calendar. Use it for calendar calculations; Duration represents time in seconds and nanoseconds. A calendar day need not equal 24 hours across a daylight-saving transition.

Period.between(start, end) accepts two non-null LocalDate values: the start is inclusive and the end exclusive. It calculates complete months and remaining days, then splits the months into years and months. An earlier end produces a negative period.

getDays() returns only the days component, not the total number of days. In the example, P1Y2M3D means one year, two months, and three days. start.plus(period) creates a new date; it does not modify start.

Examples

import java.time.LocalDate;
import java.time.Period;

public class Main {
    public static void main(String[] args) {
        LocalDate start = LocalDate.of(2023, 1, 15);
        LocalDate end = LocalDate.of(2024, 3, 18);
        Period period = Period.between(start, end);

        System.out.println(period);
        System.out.println(period.getDays());
        System.out.println(start.plus(period));
    }
}

Result

P1Y2M3D
3
2024-03-18

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources