Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator
plus
period

What is Period.plus() in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

plus() is an instance method of the Period class used to add the specified Period to the Period object on which the method is called. This method operates individually on the years, months, and days of the Period object. No normalization is performed in this method.

The plus method is defined in the Period class. The Period class is defined in the java.time package. To import the Period class, use the following import statement:

import java.time.Period;
Import statement

Syntax

public Period plus(TemporalAmount amountToAdd)
Syntax of the plus() method

Parameters

  • TemporalAmount amountToAdd: The amount to add to the Period object.

Return value

This method returns a copy of the Period object on which the method is called with the specified period added.

Code

import java.time.Period;
public class Main {
public static void main(String[] args) {
Period period1 = Period.of(4 ,5, 9);
Period period2 = Period.of(4 ,9, 24);
Period sumPeriod = period1.plus(period2);
System.out.printf("%s + %s = %s", period1, period2, sumPeriod);
}
}
Implementation of the plus() method

Explanation

  • Line 1: We import the Period class.
  • Line 6: We define the first Period object.
  • Line 7: We define the second Period object.
  • Line 9: We add the second Period object to the first one using the plus() method.
  • Line 10: We print the period objects and the sum obtained in line 9.

RELATED TAGS

java
communitycreator
plus
period

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring