Java LocalDateTime until() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 8+

Definition

In Java, start.until(end, unit) returns the number of complete units between two date-times, as a long. For example, between 10:30 on Sunday and 14:45 on Tuesday there are 52 complete hours, even though the real gap is a quarter of an hour longer.

The end is not included, and a partial unit is dropped, not rounded. The same result comes from ChronoUnit.HOURS.between(start, end).

An instance method of java.time.LocalDateTime (since Java 8): call it on a LocalDateTime object. Add import java.time.LocalDateTime; at the top of your file. It comes from Temporal, which LocalDateTime implements.

Syntax

Call it on the start and pass the end and a ChronoUnit.

Java
LocalDateTime start = LocalDateTime.of(2026, 3, 15, 10, 30);
LocalDateTime end = LocalDateTime.of(2026, 3, 17, 14, 45);

long hours = start.until(end, ChronoUnit.HOURS);   // 52
long days = start.until(end, ChronoUnit.DAYS);     // 2
CallWhat it does
start.until(end, unit)The number of complete units from start to end.
unit.between(start, end)The same result, written the other way around.
Duration.between(start, end)The exact gap as a Duration, in hours, minutes, and seconds.
Period.between(startDate, endDate)The gap as years, months, and days between two dates.

If end is before start, the result is negative.

Library declarations
Java
// java.time.LocalDateTime
public long until(Temporal endExclusive, TemporalUnit unit)

Parameters

ParameterDescription
endExclusive (Temporal)The end date-time. It is converted to a LocalDateTime, so it must have a date and a time.
unit (TemporalUnit)The unit to count in, such as ChronoUnit.DAYS or ChronoUnit.MINUTES.

Return Value

Return ValueDescription
longThe number of complete units. Negative if the end is before the start.

Compare with Similar Methods

Ways to measure the gap between two date-times:

MethodHow it differs
isAfter(other)Only tells you the order, with no distance.
plus(n, unit)Moves forward by an amount, the opposite of measuring.
compareTo(other)Gives a sign for the order, not a distance in units.

Java Examples

Example 1 — Count Hours, Days, and Minutes Between Two Date-Times

Measure the gap from Sunday 10:30 to Tuesday 14:45 in three different units.

Java
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

public class LdtUntilDemo {
    public static void main(String[] args) {
        LocalDateTime start = LocalDateTime.of(2026, 3, 15, 10, 30);
        LocalDateTime end = LocalDateTime.of(2026, 3, 17, 14, 45);

        System.out.println("days:    " + start.until(end, ChronoUnit.DAYS));
        System.out.println("hours:   " + start.until(end, ChronoUnit.HOURS));
        System.out.println("minutes: " + start.until(end, ChronoUnit.MINUTES));
    }
}

How It Works

  1. A gap of 2 days, 4 hours, 15 minutes. The start is Sunday 10:30 and the end is Tuesday 14:45.
  2. Days. Only 2 full days fit, so the answer is 2.
  3. Hours. 2 days is 48 hours, and 4 more full hours make 52. The extra 15 minutes are dropped.
  4. Minutes. 48 hours plus 4 hours is 52 hours, which is 3,120 minutes, and 15 more minutes make 3,135.

Example 2 — See That Partial Units Are Dropped

Measure the days between times that are just below and exactly on a full day.

Java
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

public class LdtUntilWhole {
    public static void main(String[] args) {
        LocalDateTime start = LocalDateTime.of(2026, 3, 15, 10, 0);
        LocalDateTime almost = LocalDateTime.of(2026, 3, 16, 9, 59);
        LocalDateTime exact = LocalDateTime.of(2026, 3, 16, 10, 0);

        System.out.println("almost a day, in days:  " + start.until(almost, ChronoUnit.DAYS));
        System.out.println("almost a day, in hours: " + start.until(almost, ChronoUnit.HOURS));
        System.out.println("exactly a day, in days: " + start.until(exact, ChronoUnit.DAYS));
    }
}

How It Works

  1. One minute short of a day. almost is 23 hours and 59 minutes after start.
  2. 0 full days. The date changed, but the time of day has not reached 10:00 yet, so no full day has passed.
  3. 23 full hours. The missing minute is dropped, not rounded up.
  4. Exactly 24 hours. exact reaches the same time of day one date later, so one full day has passed.

Example 3 — Measure Backwards and in Months

Measure from 31 January to 1 March in months and days, and then swap the two dates.

Java
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

public class LdtUntilNegative {
    public static void main(String[] args) {
        LocalDateTime jan31 = LocalDateTime.of(2026, 1, 31, 12, 0);
        LocalDateTime mar1 = LocalDateTime.of(2026, 3, 1, 12, 0);

        System.out.println("months:        " + jan31.until(mar1, ChronoUnit.MONTHS));
        System.out.println("days:          " + jan31.until(mar1, ChronoUnit.DAYS));
        System.out.println("days reversed: " + mar1.until(jan31, ChronoUnit.DAYS));
    }
}

How It Works

  1. 31 January to 1 March. Both are at noon, so no time of day gets in the way.
  2. 1 full month. One month has passed, but a second month from 31 January would only be complete at the end of March, so it is not counted.
  3. 29 days. From 31 January to 28 February is 28 days, and one more day reaches 1 March. February 2026 has 28 days.
  4. Swap the dates. Asking from the later date to the earlier one gives -29, the same number with a minus sign.

Frequently Asked Questions

Use start.until(end, ChronoUnit.HOURS), or ChronoUnit.HOURS.between(start, end). For an exact gap use Duration.between(start, end).
Neither. It drops the incomplete part, so 23 hours 59 minutes is 0 days.
The result is negative, for example -29 days.
until() gives one number in the unit you choose, cut to whole units. Duration.between() gives the full gap in hours, minutes, seconds, and nanoseconds.
Use Period.between(startDate, endDate) with two LocalDate values, for example Period.between(start.toLocalDate(), end.toLocalDate()).

Next: with()

Return a copy changed by an adjuster, or with one field replaced.

with() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful