Close

Java Date Time - Duration.between() Examples

[Last Updated: Nov 8, 2025]

Java Date Time Java Java API 


Class:

java.time.Duration

java.lang.Objectjava.lang.Objectjava.time.Durationjava.time.Durationjava.time.temporal.TemporalAmountTemporalAmountjava.lang.ComparableComparablejava.io.SerializableSerializableLogicBig

Method:

public static Duration between(Temporal startInclusive,
                               Temporal endExclusive)

Obtains a Duration object which has the time duration amount between the two temporal objects.

If the objects are of different types, then the duration is calculated based on the type of the first object.

The specified temporal objects must support the SECONDS unit.

The result of this method can be a negative period if the end is before the start.

Examples


package com.logicbig.example.duration;

import java.time.*;
import java.time.temporal.Temporal;

public class BetweenExample {

public static void main(String... args) {
btw(LocalDateTime.now(), LocalDateTime.now().withHour(23));
btw(LocalTime.now(), LocalTime.now().withMinute(59));
btw(OffsetDateTime.now(), OffsetDateTime.now().plusSeconds(59));
btw(OffsetTime.now(), OffsetTime.now().withNano(1000000));

btw(Instant.now(), Instant.now().plusSeconds(200));
btw(ZonedDateTime.now(), ZonedDateTime.now().withDayOfMonth(25));
}

private static void btw(Temporal t1, Temporal t2) {
Duration d = Duration.between(t1, t2);
System.out.printf("Btw %-15s> %-46s and %-46s > %s%n",
t1.getClass().getSimpleName(), t1, t2, d);
}
}

Output

Btw LocalDateTime  > 2025-10-29T20:14:22.685                        and 2025-10-29T23:14:22.685                        > PT3H
Btw LocalTime > 20:14:22.687 and 20:59:22.687 > PT45M
Btw OffsetDateTime > 2025-10-29T20:14:22.689+08:00 and 2025-10-29T20:15:21.689+08:00 > PT59S
Btw OffsetTime > 20:14:22.691+08:00 and 20:14:22.001+08:00 > PT-0.69S
Btw Instant > 2025-10-29T12:14:22.691Z and 2025-10-29T12:17:42.691Z > PT3M20S
Btw ZonedDateTime > 2025-10-29T20:14:22.692+08:00[America/Chicago] and 2025-10-25T20:14:22.692+08:00[America/Chicago] > PT-96H




Duration between different Temporal types. The second arg type is converted to the first one's type.

package com.logicbig.example.duration;

import java.time.*;
import java.time.temporal.Temporal;

public class BetweenExample2 {

public static void main(String... args) {
btw(LocalTime.now(), LocalDateTime.now());
btw(OffsetTime.now(), OffsetDateTime.now().minusSeconds(2));
btw(Instant.now(), OffsetDateTime.now().withNano(100));
btw(Instant.now(), ZonedDateTime.now().plusSeconds(3));
btw(LocalTime.now(), OffsetTime.now().plusSeconds(2));
btw(LocalTime.now(), OffsetDateTime.now().plusSeconds(2));
btw(LocalTime.now(), ZonedDateTime.now().plusSeconds(3));
}

private static void btw(Temporal t1, Temporal t2) {
Duration d = Duration.between(t1, t2);
System.out.printf("Btw %-10s & %-14s > %-25s and %-46s > %s%n",
t1.getClass().getSimpleName(), t2.getClass().getSimpleName(), t1, t2, d);
}
}

Output

Btw LocalTime  & LocalDateTime  > 20:14:28.838              and 2025-10-29T20:14:28.838                        > PT0S
Btw OffsetTime & OffsetDateTime > 20:14:28.843+08:00 and 2025-10-29T20:14:26.843+08:00 > PT-2S
Btw Instant & OffsetDateTime > 2025-10-29T12:14:28.845Z and 2025-10-29T20:14:28.000000100+08:00 > PT-0.8449999S
Btw Instant & ZonedDateTime > 2025-10-29T12:14:28.845Z and 2025-10-29T20:14:31.847+08:00[America/Chicago] > PT3.002S
Btw LocalTime & OffsetTime > 20:14:28.847 and 20:14:30.847+08:00 > PT2S
Btw LocalTime & OffsetDateTime > 20:14:28.847 and 2025-10-29T20:14:30.847+08:00 > PT2S
Btw LocalTime & ZonedDateTime > 20:14:28.847 and 2025-10-29T20:14:31.847+08:00[America/Chicago] > PT3S




This example shows if one of the Temporal types does not have SECONDS unit, an exception will be thrown

package com.logicbig.example.duration;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;

public class BetweenExample3 {

public static void main(String... args) {
LocalDateTime t1 = LocalDateTime.now();
LocalDate t2 = LocalDate.now();
Duration d = Duration.between(t1, t2);
System.out.printf("Duration %s%n", d);
}
}

Output

java.time.DateTimeException: Unable to obtain LocalDateTime from TemporalAccessor: 2025-10-29 of type java.time.LocalDate
at java.time.LocalDateTime.from (LocalDateTime.java:461)
at java.time.LocalDateTime.until (LocalDateTime.java:1683)
at java.time.Duration.between (Duration.java:475)
at com.logicbig.example.duration.BetweenExample3.main (BetweenExample3.java:18)
Caused by: java.time.DateTimeException: Unable to obtain LocalTime from TemporalAccessor: 2025-10-29 of type java.time.LocalDate
at java.time.LocalTime.from (LocalTime.java:409)
at java.time.LocalDateTime.from (LocalDateTime.java:457)
at java.time.LocalDateTime.until (LocalDateTime.java:1683)
at java.time.Duration.between (Duration.java:475)
at com.logicbig.example.duration.BetweenExample3.main (BetweenExample3.java:18)




See Also