Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

ChronoPeriod plus() method in Java with Examples

  • Last Updated : 24 Jun, 2019

The plus() method of ChronoPeriod interface in Java is used to add the given amount of period to the specified period. This function operates separately on YEAR, MONTH and DAY.

Note: Normalization is not performed. 12 months and 1 year are different.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

Syntax:

ChronoPeriod plus(TemporalAmount amountToAdd)

Parameters: This function accepts a single parameter amountToAdd, which is amount to add to period. It must not be null.



Returns Value This function returns a ChronoPeriod based on the given period with the requested period added, and this must not be null.

Exceptions:

  • DateTimeException: This exception is returned if the specified amount has a non-ISO chronology or contains an invalid unit.
  • ArithmeticException: This exception is caught if numeric overflow occurs.

Below programs illustrate the above method:

Program 1:




// Java code to show the function plus()
// to subtract the two given periods
  
import java.time.*;
import java.time.chrono.*;
import java.time.temporal.ChronoUnit;
  
public class ChronoPeriodDemo {
  
    // Function to subtract two given periods
    static void addChronoPeriod(ChronoPeriod p1, ChronoPeriod p2)
    {
  
        System.out.println(p1.plus(p2));
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        // Defining first period
        int year = 4;
        int months = 11;
        int days = 10;
        ChronoPeriod p1 = Period.of(year, months, days);
  
        // Defining second period
        int year1 = 2;
        int months1 = 7;
        int days1 = 8;
        ChronoPeriod p2 = Period.of(year1, months1, days1);
  
        addChronoPeriod(p1, p2);
    }
}
Output:
P6Y18M18D

Program 2: ChronoPeriod can be negative.




// Java code to show the function plus()
// to subtract the two given periods
  
import java.time.*;
import java.time.chrono.*;
import java.time.temporal.ChronoUnit;
  
public class ChronoPeriodDemo {
  
    // Function to add two given periods
    static void addChronoPeriod(ChronoPeriod p1, ChronoPeriod p2)
    {
  
        System.out.println(p1.plus(p2));
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        // Defining second period
        int year1 = 2;
        int months1 = 7;
        int days1 = 8;
        ChronoPeriod p1 = Period.of(year1, months1, days1);
  
        // Defining first period
        int year = 4;
        int months = 11;
        int days = 10;
        ChronoPeriod p2 = Period.of(year, months, days);
  
        addChronoPeriod(p1, p2);
    }
}
Output:
P6Y18M18D

Reference: https://docs.oracle.com/javase/9/docs/api/java/time/chrono/ChronoPeriod.html#plus-java.time.temporal.TemporalAmount-




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!