Duration addTo(Temporal) method in Java with Examples Last Updated : 13 Feb, 2023 Comments Improve Suggest changes Like Article Like Report The addTo(Temporal) method of Duration Class in java.time package is used add this duration to the specified temporal object, passed as the parameter. Syntax: public Temporal addTo?(Temporal temporalObject) Parameters: This method accepts a parameter temporalObject which is the amount to be adjusted in this duration. It should not be null. Return Value: This method returns an object of the same type with the temporalObject adjusted to it. Exception: This method throws: DateTimeException: if unable to add.ArithmeticException: if numeric overflow occurs. Below examples illustrate the Duration.addTo() method: Example 1: Java // Java code to illustrate Duration addTo() method import java.time.*; public class GFG { public static void main(String[] args) { // Duration 1 using parse() method Duration duration1 = Duration.parse("P2DT3H4M"); // Get the time to be adjusted LocalDateTime currentTime = LocalDateTime.now(); System.out.println("Original time: " + currentTime); // Adjust the time // using addTo() method System.out.println( duration1 .addTo(currentTime)); } } Output:Original time: 2018-11-26T07:01:13.535 2018-11-28T10:05:13.535 Example 2: Java // Java code to illustrate duration addTo() method import java.time.*; public class GFG { public static void main(String[] args) { // Duration Duration duration2 = Duration.ofDays(-5); // Get the time to be adjusted LocalDateTime currentTime = LocalDateTime.now(); System.out.println("Original time: " + currentTime); // Adjust the time // using addTo() method System.out.println( duration2 .addTo(currentTime)); } } Output:Original time: 2018-11-26T07:01:16.615 2018-11-21T07:01:16.615 Reference: Oracle Doc Comment More infoAdvertise with us Next Article Duration from(TemporalUnit) method in Java with Examples C code_r Follow Improve Article Tags : Java Java-Functions Java-time package Java-Duration Practice Tags : Java Similar Reads Duration get(TemporalUnit) method in Java with Examples The get(TemporalUnit) method of Duration Class in java.time package is used to get the value of the unit passed as the parameter. Only units SECONDS and NANOS ate supported by this method and the rest result in exception. Syntax: public long get(TemporalUnit unit) Parameters: This method accepts a p 2 min read Duration get(TemporalUnit) method in Java with Examples The get(TemporalUnit) method of Duration Class in java.time package is used to get the value of the unit passed as the parameter. Only units SECONDS and NANOS ate supported by this method and the rest result in exception. Syntax: public long get(TemporalUnit unit) Parameters: This method accepts a p 2 min read Duration from(TemporalUnit) method in Java with Examples The from(TemporalUnit) method of Duration Class in java.time package is used to get a duration from the amount passed as the first parameter in the TemporalUnit. The TemporalUnit can be DAYS, HOURS, etc. Syntax: public static Duration from(TemporalUnit amount) Parameters: This method accepts a param 1 min read Duration from(TemporalUnit) method in Java with Examples The from(TemporalUnit) method of Duration Class in java.time package is used to get a duration from the amount passed as the first parameter in the TemporalUnit. The TemporalUnit can be DAYS, HOURS, etc. Syntax: public static Duration from(TemporalUnit amount) Parameters: This method accepts a param 1 min read Duration plus(Duration) method in Java with Examples The plus(Duration) method of Duration Class in java.time package is used to get an immutable copy of this duration with the specified duration added, passed as the parameter. Syntax: public Duration plus(Duration duration) Parameters: This method accepts a parameter duration which is the duration to 2 min read Duration plus(Duration) method in Java with Examples The plus(Duration) method of Duration Class in java.time package is used to get an immutable copy of this duration with the specified duration added, passed as the parameter. Syntax: public Duration plus(Duration duration) Parameters: This method accepts a parameter duration which is the duration to 2 min read Like