Duration and Period in Java

Naveen AnamaneniUpdated · 5 min read

Duration and Period are java.time classes that represent an amount of time, not a point in time. Duration is used for time-based amounts measured in seconds and nanoseconds, such as "2 hours 30 minutes" for a movie. Period is used for date-based amounts measured in years, months and days, such as "3 years, 2 months and 5 days" for someone's age. In this lesson, you will learn how to create, calculate and use both, and build an age calculator.

Duration vs Period

DurationPeriod
MeasuresSeconds and nanosecondsYears, months and days
Works withInstant, LocalTime, LocalDateTime, ZonedDateTimeLocalDate
ExamplePT2H30M (2 hours 30 minutes)P3Y2M5D (3 years 2 months 5 days)
"One day" meansExactly 24 hoursOne calendar day (may be 23 or 25 hours across DST)
Created byDuration.ofHours(2), Duration.between(t1, t2)Period.ofYears(3), Period.between(d1, d2)

Methods

MethodDescription
Duration.ofMinutes(n), ofHours(n), ofSeconds(n)It is used to create a duration.
Duration.between(start, end)It returns the time between two times.
toHours(), toMinutes(), toMinutesPart()It reads the whole duration in a unit, or one part of it.
Period.of(y, m, d), ofMonths(n)It creates a period.
Period.between(start, end)It returns the years, months and days between two dates.
getYears(), getMonths(), getDays()It reads the parts of a period.
plus(...), multipliedBy(n)It does arithmetic on amounts.

Example: Duration

Let's see an example that measures a movie and a working day.

DurationDemo.java▶ Try it
import java.time.Duration;
import java.time.LocalTime;
 
public class DurationDemo {
    public static void main(String[] args) {
        Duration movie = Duration.ofMinutes(150);
        System.out.println("Movie: " + movie);
        System.out.println(movie.toHours() + " h "
            + movie.toMinutesPart() + " min");
 
        LocalTime in = LocalTime.of(9, 15);
        LocalTime out = LocalTime.of(17, 45);
        Duration worked = Duration.between(in, out);
        System.out.println("Worked: " + worked.toHours() + " h "
            + worked.toMinutesPart() + " min");
        System.out.println("Over 8 hours? "
            + (worked.compareTo(Duration.ofHours(8)) > 0));
 
        System.out.println("Five shifts: " + worked.multipliedBy(5));
    }
}
Output
Movie: PT2H30M
2 h 30 min
Worked: 8 h 30 min
Over 8 hours? true
Five shifts: PT42H30M

Explanation: A Duration prints in ISO format: PT2H30M means "period of time, 2 hours 30 minutes". toHours() gives the whole hours and toMinutesPart() the leftover minutes. Duration.between measures the time between two LocalTime values.

Example: Period

PeriodDemo.java▶ Try it
import java.time.LocalDate;
import java.time.Period;
 
public class PeriodDemo {
    public static void main(String[] args) {
        LocalDate joined = LocalDate.of(2023, 8, 7);
        LocalDate today = LocalDate.of(2026, 11, 12);
 
        Period served = Period.between(joined, today);
        System.out.println("Period: " + served);
        System.out.println(served.getYears() + " years, " + served.getMonths()
            + " months, " + served.getDays() + " days");
 
        LocalDate renewal = today.plus(Period.ofMonths(6));
        System.out.println("Renewal: " + renewal);
    }
}
Output
Period: P3Y3M5D
3 years, 3 months, 5 days
Renewal: 2027-05-12

Explanation: Period.between counts whole years, then months, then days between two dates. P3Y3M5D is the ISO form. Adding Period.ofMonths(6) moves the date forward by six calendar months.

The Checkpoint: An Age Calculator

AgeCalculator.java▶ Try it
import java.time.LocalDate;
import java.time.Period;
import java.time.temporal.ChronoUnit;
 
public class AgeCalculator {
    static void describe(String name, LocalDate birth, LocalDate today) {
        Period age = Period.between(birth, today);
        LocalDate next = birth.withYear(today.getYear());
        if (!next.isAfter(today)) {
            next = next.plusYears(1);
        }
        long daysToBirthday = ChronoUnit.DAYS.between(today, next);
        System.out.println(name + " is " + age.getYears() + " years, "
            + age.getMonths() + " months and " + age.getDays()
            + " days old; next birthday in " + daysToBirthday + " days");
    }
 
    public static void main(String[] args) {
        LocalDate today = LocalDate.of(2026, 11, 12);
        describe("Navika", LocalDate.of(2012, 3, 15), today);
        describe("Srihitha", LocalDate.of(2015, 11, 20), today);
        describe("Advitha", LocalDate.of(2019, 11, 12), today);
    }
}
Output
Navika is 14 years, 7 months and 28 days old; next birthday in 123 days
Srihitha is 10 years, 11 months and 23 days old; next birthday in 8 days
Advitha is 7 years, 0 months and 0 days old; next birthday in 365 days

Explanation: Period.between gives the exact age in years, months and days. To find the next birthday, the birth date is moved into this year; if that date has already passed (or is today), one year is added. ChronoUnit.DAYS.between then counts the days to it. Advitha's birthday is today, so the next one is a full year away.

Common Mistakes

Using period.getDays() as the total number of days. It's only the leftover days; use ChronoUnit.DAYS.between.

Using Duration with LocalDate. Duration.between needs time-based values; use Period or ChronoUnit for dates.

Calling toMinutes() when you want the leftover minutes. Use toMinutesPart().

Interview Corner

What is the difference between Duration and Period?

Duration is a time-based amount stored in seconds and nanoseconds, used with times and instants, for example "90 minutes". Period is a date-based amount in years, months and days, used with LocalDate, for example "2 years and 3 months". Adding a Period of one day to a ZonedDateTime keeps the same clock time across daylight-saving changes, while adding a Duration of 24 hours adds exactly 24 hours.

Practice

  1. Easy: Calculate how long a train trip lasts that leaves at 06:40 and arrives at 14:05.
  2. Harder: Extend the age calculator to print each person's age in total days and the day of the week they were born.

Quick check: Duration and Period

4 questions
1Which class measures years, months and days?
2What does PT1H15M mean?
3For Period.between(2026-01-01, 2026-03-15), what is getDays()?
4Which gives the total days between two dates?

Key Takeaways

Was this tutorial helpful?

Lesson 332 of 482