Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

ChronoUnit.DAYS.between — days between dates

ChronoUnit.DAYS.between(start, end) returns the number of complete days as a long. For LocalDate, it calculates the calendar-date difference; an end before the start produces a negative result.

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

Syntax

long days = ChronoUnit.DAYS.between(startInclusive, endExclusive);

ChronoUnit.DAYS.between(start, end) returns a long: the number of complete days between compatible temporal objects. The start is inclusive and the end exclusive. The equivalent expression is start.until(end, ChronoUnit.DAYS).

For LocalDate, it calculates the calendar-date difference. The example spans February 29, 2024: from 2024-02-28 to 2024-03-01 is 2 days, not 3. Reversing these arguments gives -2, and identical dates give 0.

The meaning of a complete day depends on the argument types. DAYS is a date-based unit; do not assume a day always means exactly 24 hours, especially across daylight-saving changes. Arguments must be non-null, compatible, and support the unit; an unsupported unit causes UnsupportedTemporalTypeException. The calculation does not modify its arguments.

Examples

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

public class Main {
    public static void main(String[] args) {
        LocalDate start = LocalDate.of(2024, 2, 28);
        LocalDate end = LocalDate.of(2024, 3, 1);

        System.out.println(ChronoUnit.DAYS.between(start, end));
        System.out.println(ChronoUnit.DAYS.between(end, start));
        System.out.println(ChronoUnit.DAYS.between(start, start));
    }
}

Result

2
-2
0

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