Returns a copy of this duration with the specified duration added.
Returns a copy of this duration with specified amount per specified unit added.
package com.logicbig.example.duration;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.time.temporal.UnsupportedTemporalTypeException;
public class PlusExample2 {
public static void main(String... args) {
Duration d = Duration.ofHours(23);
System.out.println(d);
for (ChronoUnit unit : ChronoUnit.values()) {
try {
Duration d2 = d.plus(2, unit);
System.out.printf("plus 2 %-8s unit => %s%n", unit, d2);
} catch (UnsupportedTemporalTypeException e) {
System.out.printf("%10s unit not supported%n", unit);
}
}
}
}
Output
PT23H
plus 2 Nanos unit => PT23H0.000000002S
plus 2 Micros unit => PT23H0.000002S
plus 2 Millis unit => PT23H0.002S
plus 2 Seconds unit => PT23H2S
plus 2 Minutes unit => PT23H2M
plus 2 Hours unit => PT25H
plus 2 HalfDays unit => PT47H
plus 2 Days unit => PT71H
Weeks unit not supported
Months unit not supported
Years unit not supported
Decades unit not supported
Centuries unit not supported
Millennia unit not supported
Eras unit not supported
Forever unit not supported