Java Date Time Java Java API
Class:
java.time.LocalTime
Method:
public long until(Temporal endExclusive,
TemporalUnit unit)
This method calculates the amount in provided unit between this LocalTime and the provided temporal.
In other words, this method returns the amount of time between two LocalTime objects in terms of the provided TemporalUnit
Examples
package com.logicbig.example.localtime;
import java.time.LocalTime; import java.time.temporal.ChronoUnit;
public class UntilExample {
public static void main (String... args) { LocalTime t1 = LocalTime.of(1, 15, 24); LocalTime t2 = LocalTime.of(19, 30, 40);
long l = t1.until(t2, ChronoUnit.HOURS); System.out.printf("HOURS between %s and %s => %s%n", t1, t2, l);
l = t1.until(t2, ChronoUnit.MINUTES); System.out.printf("MINUTES between %s and %s => %s%n", t1, t2, l);
l = t1.until(t2, ChronoUnit.SECONDS); System.out.printf("SECONDS between %s and %s => %s%n", t1, t2, l);
l = t1.until(t2, ChronoUnit.NANOS); System.out.printf("NANOS between %s and %s => %s%n", t1, t2, l);
l = t1.until(t2, ChronoUnit.HALF_DAYS); System.out.printf("HALF_DAYS between %s and %s => %s%n", t1, t2, l);
}
}
OutputHOURS between 01:15:24 and 19:30:40 => 18 MINUTES between 01:15:24 and 19:30:40 => 1095 SECONDS between 01:15:24 and 19:30:40 => 65716 NANOS between 01:15:24 and 19:30:40 => 65716000000000 HALF_DAYS between 01:15:24 and 19:30:40 => 1
ChronoUnit.DAYS is not supported. package com.logicbig.example.localtime;
import java.time.LocalTime; import java.time.temporal.ChronoUnit;
public class UntilExample2 {
public static void main (String... args) { LocalTime t1 = LocalTime.of(1, 15, 24); LocalTime t2 = LocalTime.of(19, 30, 40);
long l = t1.until(t2, ChronoUnit.DAYS); System.out.printf("DAYS between %s and %s => %s%n", t1, t2, l); }
}
OutputCaused by: java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Days at java.time.LocalTime.until(LocalTime.java:1397) at com.logicbig.example.localtime.UntilExample2.main(UntilExample2.java:18) ... 6 more
|
|