This method returns a new instance of OffsetTime, subtracted by the provided amount per provided TemporalUnit.
package com.logicbig.example.offsettime;
import java.time.Duration;
import java.time.LocalTime;
import java.time.OffsetTime;
import java.time.ZoneOffset;
public class MinusExample {
public static void main(String... args) {
OffsetTime t1 = OffsetTime.of(LocalTime.of(10, 22, 10), ZoneOffset.of("-06:00"));
System.out.println(t1);
OffsetTime t2 = t1.minus(Duration.ofHours(24));
System.out.println(t2);
OffsetTime t3 = t1.minus(Duration.ofHours(5));
System.out.println(t3);
OffsetTime t4 = t1.minus(Duration.ofHours(-5));
System.out.println(t4);
}
}
Output
10:22:10-06:00
10:22:10-06:00
05:22:10-06:00
15:22:10-06:00
package com.logicbig.example.offsettime;
import java.time.DateTimeException;
import java.time.LocalTime;
import java.time.OffsetTime;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
public class MinusExample2 {
public static void main(String... args) {
OffsetTime t1 = OffsetTime.of(LocalTime.of(10, 22, 10), ZoneOffset.of("-06:00"));
System.out.println(t1);
for (ChronoUnit chronoUnit : ChronoUnit.values()) {
try {
OffsetTime minus = t1.minus(4, chronoUnit);
System.out.printf("%10s: %s%n", chronoUnit.name(), minus);
} catch (DateTimeException e) {
System.out.printf("%10s: not supported%n", chronoUnit.name());
}
}
}
}
Output
10:22:10-06:00
NANOS: 10:22:09.999999996-06:00
MICROS: 10:22:09.999996-06:00
MILLIS: 10:22:09.996-06:00
SECONDS: 10:22:06-06:00
MINUTES: 10:18:10-06:00
HOURS: 06:22:10-06:00
HALF_DAYS: 10:22:10-06:00
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