java.time.Period Class



Introduction

The java.time.Period class models a quantity or amount of time in terms of years, months and days.

Class declaration

Following is the declaration for java.time.Period class −

public final class Period
   extends Object
      implements ChronoPeriod, Serializable

Field

Following are the fields for Java.time.Period class −

  • static Period ZERO − Constant for a Period of zero.

Class methods

Sr.No. Method & Description
1 Temporal addTo(Temporal temporal)

Adds this Period to the specified temporal object.

2 static Period between(LocalDate startInclusive, LocalDate endExclusive)

Obtains a Period consisting of the number of years, months, and days between two dates.

3 boolean equals(Object otherPeriod)

Checks if this Period is equal to the specified Period.

4 static Duratio from(TemporalAmount amount)

Obtains an instance of Period from a temporal amount.

5 long get(TemporalUnit unit)

Gets the value of the requested unit.

6 IsoChronology getChronology()

Gets the chronology of this period, which is the ISO calendar system.

7 int getDays()

Gets the amount of days of this period.

8 int getMonths()

Gets the amount of months of this period.

9 List<TemporalUnit> getUnits()

Gets the set of units supported by this Period.

10 int hashCode()

A hash code for this Period.

11 boolean isNegative()

Checks if this Period is negative, excluding zero.

12 boolean isZero()

Checks if this Period is zero length.

13 Period minus(TemporalAmount amountToSubtract)

Returns a copy of this Period with the specified Period subtracted.

14 Period minusDays(long daysToSubtract)

Returns a copy of this period with the specified days subtracted.

15 Period minusMonths(long months)

Returns a copy of this period with the specified months subtracted.

16 Period minusYears(long years)

Returns a copy of this period with the specified years subtracted.

17 Period multipliedBy(long multiplicand)

Returns a copy of this Period multiplied by the scalar.

18 Period negated()

Returns a copy of this Period with the length negated.

19 Period normalized()

Returns a copy of this period with the years and months normalized.

20 static Period of(int years, int months, int days)

Obtains a Period representing a number of years, months and days.

21 static Period ofDays(int days)

Obtains a Period representing a number of days.

22 static Period ofMonths(int months)

Obtains a Period representing a number of months.

23 static Period ofWeeks(int weeks)

Obtains a Period representing a number of weeks.

24 static Period ofYears(int years)

Obtains a Period representing a number of weeks.

25 static Period parse(CharSequence text)

Obtains a Period from a text string such as PnYnMnD.

26 Period plus(TemporalAmount amountToAdd)

Returns a copy of this Period with the specified Period added.

27 Period plusDays(long daysToAdd)

Returns a copy of this period with the specified days added.

28 Period plusMonths(long monthsToAdd)

Returns a copy of this period with the specified months added.

29 Period plusYears(long yearsToAdd)

Returns a copy of this period with the specified years added.

30 Temporal subtractFrom(Temporal temporal)

Subtracts this Period from the specified temporal object.

31 String toString()

A string representation of this Period using ISO-8601 seconds based representation, such as PT8H6M12.345S.

32 long toTotalMonths()

Gets the total number of months in this period.

33 Period withDays(int days)

Returns a copy of this period with the specified amount of days.

34 Period withMonths(int months)

Returns a copy of this period with the specified amount of months.

35 Period withYears(int years)

Returns a copy of this period with the specified amount of Years.

Methods inherited

This class inherits methods from the following classes −

  • Java.lang.Object
Advertisements