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.
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 Âimportjava.time.*;importjava.time.chrono.*;importjava.time.temporal.ChronoUnit; ÂpublicclassChronoPeriodDemo { Â    // Function to subtract two given periods    staticvoidaddChronoPeriod(ChronoPeriod p1, ChronoPeriod p2)    { Â        System.out.println(p1.plus(p2));    } Â    // Driver Code    publicstaticvoidmain(String[] args)    {        // Defining first period        intyear = 4;        intmonths = 11;        intdays = 10;        ChronoPeriod p1 = Period.of(year, months, days); Â        // Defining second period        intyear1 = 2;        intmonths1 = 7;        intdays1 = 8;        ChronoPeriod p2 = Period.of(year1, months1, days1); Â        addChronoPeriod(p1, p2);    }} | 
P6Y18M18D
Program 2: ChronoPeriod can be negative.
| // Java code to show the function plus()// to subtract the two given periods Âimportjava.time.*;importjava.time.chrono.*;importjava.time.temporal.ChronoUnit; ÂpublicclassChronoPeriodDemo { Â    // Function to add two given periods    staticvoidaddChronoPeriod(ChronoPeriod p1, ChronoPeriod p2)    { Â        System.out.println(p1.plus(p2));    } Â    // Driver Code    publicstaticvoidmain(String[] args)    {        // Defining second period        intyear1 = 2;        intmonths1 = 7;        intdays1 = 8;        ChronoPeriod p1 = Period.of(year1, months1, days1); Â        // Defining first period        intyear = 4;        intmonths = 11;        intdays = 10;        ChronoPeriod p2 = Period.of(year, months, days); Â        addChronoPeriod(p1, p2);    }} | 
P6Y18M18D

 
                                    







