Java SE API

Duration.between()

Measure the signed time-based interval between compatible temporals. Java SE API; example uses Java 21-compatible syntax.

Formal Syntax & Structure

java syntaxjava
static Duration between(Temporal startInclusive, Temporal endExclusive)

Parameters & Attributes

NameTypeDescription
startInclusiveTemporalStart temporal
endExclusiveTemporalEnd temporal

Return Value

Duration

Measure the signed time-based interval between compatible temporals.

Working Example

Example Snippetjava
import java.math.*;import java.time.*;import java.time.format.*;import java.time.temporal.*;import java.text.*;import java.util.*;public class Main {    public static void main(String[] args) throws Exception {        Instant start = Instant.parse("2024-01-01T10:00:00Z");        Instant end = Instant.parse("2024-01-01T11:35:30Z");        Duration elapsed = Duration.between(start, end);        System.out.println(elapsed);        System.out.println(elapsed.toMinutes());        System.out.println(elapsed.toSeconds());        System.out.println(Duration.between(end,start).isNegative());    }}
Java Reference