Java Period class

Java Period class is used to measures time in years, months and days. It inherits the Object class and implements the ChronoPeriod interface.


Java Period class declaration

Let's see the declaration of java.time.Period class.

Methods of Java Period

Method Description
Temporal addTo(Temporal temporal) It is used to add this period to the specified temporal object.
long get(TemporalUnit unit) It is used to get the value of the requested unit.
int getYears() It is used to get the amount of years of this period.
boolean isZero() It is used to check if all three units of this period are zero.
Period minus(TemporalAmount amountToSubtract) It is used to return a copy of this period with the specified period subtracted.
static Period of(int years, int months, int days) It is used to obtain a Period representing a number of years, months and days.
Period plus(TemporalAmount amountToAdd) It is used to return a copy of this period with the specified period added.

Java Period Example: addTo()

Test it Now

Output:

2017-02-24

Java Period Example: of()

Test it Now

Output:

P2017Y2M16D

Java Period Example: minus()

Test it Now

Output:

P2M

Java Period Example: plus()

Test it Now

Output:

P6M
Next TopicJava Duration




Contact US

Email:[email protected]

Java Period
10/30