java.time.Period.plus() Method Example



Description

The java.time.Period.plus(TemporalAmount amountToAdd) method returns a copy of this Period with the specified Period added.

Declaration

Following is the declaration for java.time.Period.plus(TemporalAmount amountToAdd) method.

public Period plus(TemporalAmount amountToAdd)

Parameters

amountToAdd − the Period to add, positive or negative, not null.

Return Value

a Period based on this Period with the specified Period added, not null.

Exception

  • DateTimeException − if the specified amount has a non-ISO chronology or contains an invalid unit.

  • ArithmeticException − if numeric overflow occurs.

Example

The following example shows the usage of java.time.Period.plus(TemporalAmount amountToAdd) method.

package com.tutorialspoint;

import java.time.Period;

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

      Period period = Period.of(1,5,2);
      System.out.println("Years: " + period.getYears() 
         + ", Months: " + period.getMonths()
         +", Days: " + period.getDays());   
      Period period1 = period.plus(Period.ofDays(5));
      System.out.println("Years: " + period1.getYears() 
         + ", Months: " + period1.getMonths()
         +", Days: " + period1.getDays());  
  
   }
}

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

Years: 1, Months: 5, Days: 2
Years: 1, Months: 5, Days: 7
Advertisements