Peroid class

The java.time.Period class represents a date-based amount of time in terms of years, months, and days. It is used to model calendar-based differences, like “2 years and 3 months” or “10 days”.

Commonly Used Methods

Simple Program

import java.time.LocalDate;
import java.time.Period;

public class SimplePeriodExample {
    public static void main(String[] args) {
        LocalDate startDate = LocalDate.of(2022, 1, 15);
        LocalDate endDate = LocalDate.of(2025, 5, 22);

        Period period = Period.between(startDate, endDate);

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

Problem Statement

LotusJavaPrince and Mahesh are building a loan management system for Sunrise Microfinance. The requirement is:

  • When a customer applies for a loan, the system should calculate the time elapsed since the customer joined the bank.
  • Based on the membership period (in years and months), the customer may be eligible for a loyalty bonus.
  • The system must use Period to evaluate membership duration accurately.
import java.time.LocalDate;
import java.time.Period;

public class LoanLoyaltyCalculator {

    public static void main(String[] args) {
        // Customer joined date
        LocalDate joiningDate = LocalDate.of(2018, 3, 12);
        LocalDate currentDate = LocalDate.now();

        // Calculate period between joining date and now
        Period membershipPeriod = Period.between(joiningDate, currentDate);

        System.out.println("Customer joined on: " + joiningDate);
        System.out.println("Current date: " + currentDate);
        System.out.println("Membership duration: " + 
            membershipPeriod.getYears() + " years, " +
            membershipPeriod.getMonths() + " months, and " +
            membershipPeriod.getDays() + " days.");

        // Loyalty bonus logic
        if (membershipPeriod.getYears() >= 5) {
            System.out.println("Eligible for Platinum Loyalty Bonus.");
        } else if (membershipPeriod.getYears() >= 3) {
            System.out.println("Eligible for Gold Loyalty Bonus.");
        } else {
            System.out.println("Not eligible for loyalty bonus yet.");
        }
    }
}
/*
Customer joined on: 2018-03-12
Current date: 2025-05-22
Membership duration: 7 years, 2 months, and 10 days.
Eligible for Platinum Loyalty Bonus.
*/

The Period class in Java is ideal for handling calendar-based differences like “years, months, and days”. It is:

  • Best suited for human-readable time spans, like age, membership length, or tenure.
  • Essential when calculating date differences where months and years matter (e.g., “2 years and 6 months”).
  • Immutable, reliable, and easily integrated with LocalDate.

Use Period for date-related logic involving years, months, and days, such as age calculation, policy durations, or eligibility tracking in banking or HR applications.

Scroll to Top