Duration and Period in Java
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
Duration | Period | |
|---|---|---|
| Measures | Seconds and nanoseconds | Years, months and days |
| Works with | Instant, LocalTime, LocalDateTime, ZonedDateTime | LocalDate |
| Example | PT2H30M (2 hours 30 minutes) | P3Y2M5D (3 years 2 months 5 days) |
| "One day" means | Exactly 24 hours | One calendar day (may be 23 or 25 hours across DST) |
| Created by | Duration.ofHours(2), Duration.between(t1, t2) | Period.ofYears(3), Period.between(d1, d2) |
Methods
| Method | Description |
|---|---|
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.
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));
}
}Movie: PT2H30M
2 h 30 min
Worked: 8 h 30 min
Over 8 hours? true
Five shifts: PT42H30MExplanation: 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
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);
}
}Period: P3Y3M5D
3 years, 3 months, 5 days
Renewal: 2027-05-12Explanation: 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
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);
}
}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 daysExplanation: 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
- Easy: Calculate how long a train trip lasts that leaves at 06:40 and arrives at 14:05.
- Harder: Extend the age calculator to print each person's age in total days and the day of the week they were born.
Key Takeaways
Was this tutorial helpful?