Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Java reference

ChronoUnit.between()

Counts the whole units (days, months, hours) between two dates or moments.

Available since: Java 8

Syntax

public long between(Temporal start, Temporal end)

Parameters

ParameterTypeDescription
startTemporalThe start — a LocalDate, LocalDateTime, Instant and so on.
endTemporalThe end, of the same type; exclusive.

Return value

longThe number of units; negative if end is before start.

Examples

How many days until an event:

void main() {
    LocalDate today = LocalDate.of(2025, 3, 14);
    LocalDate newYear = LocalDate.of(2026, 1, 1);
    IO.println(ChronoUnit.DAYS.between(today, newYear));
    IO.println(ChronoUnit.WEEKS.between(today, newYear));
    IO.println(ChronoUnit.MONTHS.between(today, newYear));
}

Output:

293
41
9

Partial units are dropped:

void main() {
    LocalDateTime a = LocalDateTime.of(2025, 3, 14, 9, 0);
    LocalDateTime b = LocalDateTime.of(2025, 3, 14, 11, 59);
    IO.println(ChronoUnit.HOURS.between(a, b));
    IO.println(ChronoUnit.MINUTES.between(a, b));
}

Output:

2
179

Notes

The same can be written as start.until(end, ChronoUnit.DAYS). Unlike Period, the result is a single number in the chosen unit.

See also

  • Period — A calendar amount in years, months and days — for example, an age.
  • Duration — An amount of time in seconds and nanoseconds — hours, minutes, seconds.
  • LocalDate.of() — Creates a date from a year, month and day.

Related recipes

In the tutorial

  • Dates and times — The java.time package — LocalDate, LocalTime, LocalDateTime, date arithmetic, Period, Duration, ChronoUnit, time zones and Instant.

Thanks for reading!

Donate to the site — we work hard for you.