Close

Java Date Time - Duration.between() Examples

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  > 2017-05-01T15:59:43.137                        and 2017-05-01T23:59:43.172                        > PT8H0.035S
Btw LocalTime > 15:59:43.174 and 15:59:43.174 > PT0S
Btw OffsetDateTime > 2017-05-01T15:59:43.174-05:00 and 2017-05-01T16:00:42.174-05:00 > PT59S
Btw OffsetTime > 15:59:43.175-05:00 and 15:59:43.001-05:00 > PT-0.174S
Btw Instant > 2017-05-01T20:59:43.175Z and 2017-05-01T21:03:03.175Z > PT3M20S
Btw ZonedDateTime > 2017-05-01T15:59:43.179-05:00[America/Chicago] and 2017-05-25T15:59:43.179-05:00[America/Chicago] > PT576H




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  > 15:59:45.190              and 2017-05-01T15:59:45.226                        > PT0.036S
Btw OffsetTime & OffsetDateTime > 15:59:45.228-05:00 and 2017-05-01T15:59:43.229-05:00 > PT-1.999S
Btw Instant & OffsetDateTime > 2017-05-01T20:59:45.229Z and 2017-05-01T15:59:45.000000100-05:00 > PT-0.2289999S
Btw Instant & ZonedDateTime > 2017-05-01T20:59:45.233Z and 2017-05-01T15:59:48.233-05:00[America/Chicago] > PT3S
Btw LocalTime & OffsetTime > 15:59:45.233 and 15:59:47.233-05:00 > PT2S
Btw LocalTime & OffsetDateTime > 15:59:45.234 and 2017-05-01T15:59:47.234-05:00 > PT2S
Btw LocalTime & ZonedDateTime > 15:59:45.234 and 2017-05-01T15:59:48.234-05: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

Caused by: java.time.DateTimeException: Unable to obtain LocalDateTime from TemporalAccessor: 2017-05-01 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)
... 6 more
Caused by: java.time.DateTimeException: Unable to obtain LocalTime from TemporalAccessor: 2017-05-01 of type java.time.LocalDate
at java.time.LocalTime.from(LocalTime.java:409)
at java.time.LocalDateTime.from(LocalDateTime.java:457)
... 9 more




See Also