Javatpoint Logo
Javatpoint Logo

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.
static Period between(LocalDate startDateInclusive, LocalDate endDateExclusive) It obtains a Period consisting of the number of years, months, and days between two dates.
boolean equals(Object obj) It checks if this period is equal to another period.
IsoChronology getChronology() It gets the chronology of this period, which is the ISO calendar system.
int getDays() It gets the amount of days of this period.
int getMonths() It gets the amount of months of this period.
List getUnits() It gets the set of units supported by this period.
Period multipliedBy(int scalar) It returns a new instance with each element in this period multiplied by the specified scalar.
Period normalized() It returns a copy of this period with the years and months normalized.
Period plusDays(long daysToAdd) It returns a copy of this period with the specified days added.
Period plusMonths(long monthsToAdd) It returns a copy of this period with the specified months added.
Period plusYears(long yearsToAdd) It returns a copy of this period with the specified years added.
Temporal subtractFrom(Temporal temporal) It subtracts this period from the specified temporal object.
Period negated() It returns a new instance with each amount in this period negated.

Java Period Example: addTo()

PeriodExample1.java

Test it Now

Output:

2017-02-24

Java Period Example: of()

PeriodExample2.java

Test it Now

Output:

P2017Y2M16D

Java Period Example: minus()

PeriodExample3.java

Test it Now

Output:

P2M

Java Period Example: plus()

PeriodExample4.java

Test it Now

Output:

P6M

Next TopicJava Duration





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA