java.time.Period.addTo() Method Example



Description

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

Declaration

Following is the declaration for java.time.Period.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.Period.addTo() method.

package com.tutorialspoint;

import java.time.LocalDateTime;
import java.time.Period;

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

      Period period = Period.of(1,1,1);

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

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

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

2017-03-22T16:44:43.176
2018-04-23T16:44:43.176
Advertisements