java.time.Period Class in Java

Last Updated : 13 Jul, 2026

The Period class in the java.time package, introduced in Java 8, represents a date-based amount of time in terms of years, months, and days. It is commonly used to calculate the difference between two dates or to add and subtract date-based values from a date.

  • Represents a period using years, months, and days.
  • Immutable and thread-safe.
  • Useful for date calculations such as age, subscription duration, and project timelines.
Java
import java.time.LocalDate;
import java.time.Period;

public class Geeks {

    public static void main(String[] args) {

        LocalDate start = LocalDate.of(2022, 1, 1);
        LocalDate end = LocalDate.of(2025, 7, 9);

        Period period = Period.between(start, end);

        System.out.println("Years : " + period.getYears());
        System.out.println("Months: " + period.getMonths());
        System.out.println("Days  : " + period.getDays());
    }
}

Output
Years : 3
Months: 6
Days  : 8

Explanation: The between() method calculates the difference between two dates and returns it as a Period object containing years, months, and days.

Syntax

public final class Period extends Object implements ChronoPeriod, Serializable

Below all the methods with the action performed by them are as follows in the tabular format are as follows:

MethodDescription
addTo(Temporal temporal)This method adds this period to the specified temporal object.
between(LocalDate startDateInclusive, LocalDate endDateExclusive)This method obtains a Period consisting of the number of years, months, and days between two dates.
equals(Object obj)This method checks if this period is equal to another period.
from(TemporalAmount amount)This method obtains an instance of Period from a temporal amount.
get(TemporalUnit unit)This method gets the value of the requested unit.
getChronology()This method gets the chronology of this period, which is the ISO calendar system.
getDays()This method gets the number of days of this period.
getMonths()This method gets the number of months of this period.
getUnits()This method gets the set of units supported by this period.
getYears()This method gets the number of years of this period.
hashCode()This method returns a hash code for this period.
isNegative()This method checks if any of the three units of this period are negative.
isZero()This method checks if all three units of this period are zero.
minus(TemporalAmount amountToSubtract)This method returns a copy of this period with the specified period subtracted.
minusDays(long daysToSubtract)This method returns a copy of this period with the specified days subtracted.
minusMonths(long monthsToSubtract)This method returns a copy of this period with the specified months subtracted.
minusYears(long yearsToSubtract)This method returns a copy of this period with the specified years subtracted.
multipliedBy(int scalar)This method returns a new instance with each element in this period multiplied by the specified scalar.
negated()This method returns a new instance with each amount in this period negated.
normalized()This method returns a copy of this period with the years and months normalized.
of(int years, int months, int days)This method obtains a Period representing a number of years, months, and days.
ofDays(int days)This method obtains a Period representing a number of days.
ofMonths(int months)This method obtains a Period representing a number of months.
ofWeeks(int weeks)This method obtains a Period representing a number of weeks.
ofYears(int years)This method obtains a Period representing a number of years.
parse(CharSequence text)This method obtains a Period from a text string such as PnYnMnD.
plus(TemporalAmount amountToAdd)This method returns a copy of this period with the specified period added.
plusDays(long daysToAdd)This method returns a copy of this period with the specified days added.
plusMonths(long monthsToAdd)This method returns a copy of this period with the specified months added.
plusYears(long yearsToAdd)This method returns a copy of this period with the specified years added.
subtractFrom(Temporal temporal)This method subtracts this period from the specified temporal object.
toString()This method outputs this period as a String, such as P6Y3M1D.
toTotalMonths()This method gets the total number of months in this period.
withDays(int days)This method returns a copy of this period with the specified amount of days. 
withMonths(int months)This method returns a copy of this period with the specified amount of months.
withYears(int years)This method returns a copy of this period with the specified amount of years.

Approach

  • Import the java.time.Period and java.time.LocalDate classes.
  • Create a Period object using methods such as of(), ofDays(), or between().
  • Perform date calculations or modify existing periods.
  • Display the resulting period or updated date.

Example 1: Using ofMonths() and minus()

Java
import java.time.Period;

public class Geeks {

    public static void main(String[] args) {

        // Create a period of 6 months
        Period p1 = Period.ofMonths(6);

        // Subtract 2 months
        Period p2 = p1.minus(Period.ofMonths(2));

        System.out.println(p2);
    }
}

Output
P4M

Explanation: The ofMonths(6) method creates a period of 6 months. The minus() method subtracts 2 months, resulting in a period of 4 months (P4M).

Example 2: Using ofDays() and addTo()

Java
import java.time.*;
import java.time.temporal.Temporal;

public class Geeks {

    public static void main(String[] args) {

        // Create a period of 24 days
        Period period = Period.ofDays(24);

        // Add the period to the current date
        Temporal result = period.addTo(LocalDate.now());

        System.out.println(result);
    }
}

Output
2026-08-02

Explanation: The ofDays(24) method creates a period of 24 days. The addTo() method adds this period to the current date and returns the updated date.

Advantages of Period Class

  • Represents date-based durations using years, months, and days.
  • Simplifies date calculations between two dates.
  • Immutable and thread-safe.
  • Provides convenient methods for adding and subtracting date-based values.
  • Integrates seamlessly with other classes in the java.time API.
Comment