Period plus() method in Java with Examples

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

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

Syntax:



public Period 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 Period 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to show the function plus()
// to subtract the two given periods
  
import java.time.Period;
import java.time.temporal.ChronoUnit;
  
public class PeriodDemo {
  
    // Function to subtract two given periods
    static void addPeriod(Period p1, Period 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;
        Period p1 = Period.of(year, months, days);
  
        // Defining second period
        int year1 = 2;
        int months1 = 7;
        int days1 = 8;
        Period p2 = Period.of(year1, months1, days1);
  
        addPeriod(p1, p2);
    }
}

chevron_right


Output:

P6Y18M18D

Program 2: Period can be negative.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to show the function plus()
// to subtract the two given periods
  
import java.time.Period;
import java.time.temporal.ChronoUnit;
  
public class PeriodDemo {
  
    // Function to add two given periods
    static void addPeriod(Period p1, Period 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;
        Period p1 = Period.of(year1, months1, days1);
  
        // Defining first period
        int year = 4;
        int months = 11;
        int days = 10;
        Period p2 = Period.of(year, months, days);
  
        addPeriod(p1, p2);
    }
}

chevron_right


Output:

P6Y18M18D

Reference: https://docs.oracle.com/javase/8/docs/api/java/time/Period.html#plus-java.time.temporal.TemporalAmount-



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.