CKTIS / Java
Duration — elapsed time and differences between instants
java.time.Duration represents an amount of time in seconds and nanoseconds. Duration.between calculates the difference between compatible temporal objects; the result can be positive, zero, or negative.
Version: Java SE 21; repository runtime, production runtime not attested
Syntax
Duration.between(Temporal startInclusive, Temporal endExclusive)java.time.Duration is an immutable, thread-safe amount of time, not a date or an instant. It stores seconds and a nanosecond fraction. Unlike the calendar-based Period, a day in Duration always means 24 hours, without accounting for daylight-saving changes.
Duration.between(startInclusive, endExclusive) calculates the difference from the inclusive start to the exclusive end. Arguments must support SECONDS; full precision also requires NANOS or NANO_OF_SECOND. When the arguments have different types, the first argument’s type determines how the second is interpreted. A LocalDateTime start can accept an OffsetDateTime end, ignoring its UTC offset. The reverse order throws DateTimeException because LocalDateTime has no UTC offset. Incompatible objects can cause DateTimeException, while overflow causes ArithmeticException.
The example uses fixed Instant values, so it does not depend on the clock or default time zone. The result PT1H30M45S means 1 hour, 30 minutes, and 45 seconds. toMinutes() returns the total number of whole minutes, namely 90, not the minute component 30. In this example, swapping the arguments gives a negative duration.
Examples
import java.time.Duration;
import java.time.Instant;
public class Main {
public static void main(String[] args) {
Instant start = Instant.parse("2025-01-01T10:00:00Z");
Instant end = Instant.parse("2025-01-01T11:30:45Z");
Duration elapsed = Duration.between(start, end);
System.out.println(elapsed);
System.out.println(elapsed.toMinutes());
System.out.println(Duration.between(end, start).isNegative());
}
}Result
PT1H30M45S 90 true
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.