Close

Java Date Time - OffsetTime.until() Examples

Java Date Time Java Java API 


Class:

java.time.OffsetTime

java.lang.Objectjava.lang.Objectjava.time.OffsetTimejava.time.OffsetTimejava.time.temporal.TemporalTemporaljava.time.temporal.TemporalAdjusterTemporalAdjusterjava.lang.ComparableComparablejava.io.SerializableSerializableLogicBig

Method:

public long until(Temporal endExclusive,
                  TemporalUnit unit)

Returns the amount of time between this instance and the provided 'endExclusive' temporal in terms of the specified unit.


Examples


package com.logicbig.example.offsettime;

import java.time.OffsetTime;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
import java.time.temporal.UnsupportedTemporalTypeException;

public class UntilExample {

public static void main(String... args) {
OffsetTime t1 = OffsetTime.of(10, 30, 23, 10000,
ZoneOffset.ofHours(-6));

OffsetTime t2 = OffsetTime.of(22, 15, 34, 40000,
ZoneOffset.ofHours(-5));

System.out.println("t1: " + t1);
System.out.println("t2: " + t2);

for (ChronoUnit chronoUnit : ChronoUnit.values()) {
try {
long l = t1.until(t2, chronoUnit);
System.out.printf("For unit %8s: %s%n", chronoUnit, l);
} catch (UnsupportedTemporalTypeException e) {
System.out.printf(" -- %s not supported%n", chronoUnit);
}
}
}
}

Output

t1: 10:30:23.000010-06:00
t2: 22:15:34.000040-05:00
For unit Nanos: 38711000030000
For unit Micros: 38711000030
For unit Millis: 38711000
For unit Seconds: 38711
For unit Minutes: 645
For unit Hours: 10
For unit HalfDays: 0
-- Days not supported
-- Weeks not supported
-- Months not supported
-- Years not supported
-- Decades not supported
-- Centuries not supported
-- Millennia not supported
-- Eras not supported
-- Forever not supported




See Also