java.time.Duration.addTo() Method Example



Description

The java.time.Duration.addTo() method adds this duration to the specified temporal object.

Declaration

Following is the declaration for java.time.Duration.addTo() method.

public Temporal addTo(Temporal temporal)

Parameters

temporal − the temporal object to adjust, not null.

Return Value

an object of the same type with the adjustment made, not null.

Exception

  • DateTimeException − if unable to add.

  • ArithmeticException − if numeric overflow occurs.

Example

The following example shows the usage of java.time.Duration.addTo() method.

package com.tutorialspoint;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.LocalTime;

public class DurationDemo {
   public static void main(String[] args) {

      Duration duration = Duration.between(LocalTime.NOON,LocalTime.MAX); 

      LocalDateTime date = LocalDateTime.now();
      System.out.println(date);  

      date = (LocalDateTime)duration.addTo(date);
      System.out.println(date);  
   }
}

Let us compile and run the above program, this will produce the following result −

2017-03-07T15:45:39.456
2017-03-08T03:45:39.455999999
Advertisements