The short answer to how to calculate age in Java is one line: Period.between(birthDate, today).getYears(). It returns the number of whole years between a date of birth and today, counting only birthdays that have already happened. A person born on 15 May 1990 is 36 on 26 November 2026, and a person born on 29 February 2000 is 26.

Table of Contents
This guide shows you how to calculate age in Java properly — with code compiled and run on a real JDK 21, so every output below is a real terminal output, not a guessed one. It covers whole years, the full years-months-days breakdown, ChronoUnit totals, the year-subtraction bug, the leap-day edge case, and the legacy Calendar trap — all on the same two birth dates as this site’s Python, JavaScript and SQL guides.
The Short Answer: Period.between in One Line
Java 8 introduced the java.time package, still the right tool in Java 21. The class you need is LocalDate — a date with no time and no time zone — and the class that measures the gap between two dates is Period.
Everything in this guide builds on that one call, so once you understand it, you know how to calculate age in Java for any app — a sign-up form, a profile page, an age check, or a report. In real code use LocalDate.now() for today; the examples use a fixed date, LocalDate.of(2026, 11, 26), so outputs stay stable and checkable.
Method 1: Whole Years with Period.between
This is the method most developers reach for first when they need to work out how to calculate age in Java: it is short, readable and correct. Period.between() counts whole years up to the last birthday, then whole months, then the leftover days, handling leap years and uneven months by itself.
import java.time.LocalDate;
import java.time.Period;
LocalDate birthDate = LocalDate.of(1990, 5, 15);
LocalDate today = LocalDate.of(2026, 11, 26);
Period age = Period.between(birthDate, today);
System.out.println(age.getYears()); // whole years only
Verified output:
36
The same call gives 26 for a leap-day birthday, born 29 February 2000.
To get the age on another date — past or future, such as 1 January 2030 — pass that date as the second argument instead of today.
Method 2: The Full Breakdown in Years, Months and Days
Whole years are enough for most apps, but profile pages and medical records usually want the full breakdown. The same Period already holds it — read all three parts with getYears(), getMonths() and getDays().
import java.time.LocalDate;
import java.time.Period;
LocalDate today = LocalDate.of(2026, 11, 26);
Period age = Period.between(LocalDate.of(1990, 5, 15), today);
System.out.println(age.getYears() + " years, " + age.getMonths() + " months, " + age.getDays() + " days");
System.out.println(age);
Period leapling = Period.between(LocalDate.of(2000, 2, 29), today);
System.out.println(leapling.getYears() + " years, " + leapling.getMonths() + " months, " + leapling.getDays() + " days");
System.out.println(leapling);
Verified output:
36 years, 6 months, 11 days
P36Y6M11D
26 years, 8 months, 28 days
P26Y8M28D
The second line is the Period printed directly, in ISO-8601 format (P36Y6M11D) — handy for logs, but show users the formatted words.
Watch out for the common Period mistake: getDays() is only the days part, not the total — here it is 11, while the total age in days is 13,344. For the total, use ChronoUnit.DAYS.between() from the next section.
Check it by hand: 36th birthday 15 May 2026, plus six months is 15 November 2026, plus eleven days is 26 November 2026. The leapling’s breakdown is trickier because 29 February does not exist in 2026 — the Feb 29 section below explains it.
Method 3: Totals with ChronoUnit — Years, Months or Days
Period gives you the age split into parts. ChronoUnit gives you the age as one total number in a single unit. Both are in the java.time family, and for whole years they agree exactly.
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
LocalDate birthDate = LocalDate.of(1990, 5, 15);
LocalDate today = LocalDate.of(2026, 11, 26);
System.out.println(ChronoUnit.YEARS.between(birthDate, today));
System.out.println(ChronoUnit.MONTHS.between(birthDate, today));
System.out.println(ChronoUnit.WEEKS.between(birthDate, today));
System.out.println(ChronoUnit.DAYS.between(birthDate, today));
Verified output:
36
438
1906
13344
If you only need one number, ChronoUnit is the cleanest way to handle how to calculate age in Java in years, months or days. The totals are exact counts, not averages; the 13,344 days cross-checks with date -d.
The same call gives 9,767 days for the leap-day birthday, also cross-checked with date -d. If your app needs the age in days, this is the method to use — our guide to how to calculate age in days explains the day-counting conventions behind the number.
The Year-Subtraction Bug
The bug is the first thing to check whenever someone asks how to calculate age in Java by subtracting years. It looks right, and it is wrong for part of every year:
int naiveAge = 2026 - 1990; // 36
That gives 36 for everyone born in 1990 — including someone born on 15 December 1990, whose birthday has not happened yet on 26 November 2026. The true age is 35:
import java.time.LocalDate;
import java.time.Period;
LocalDate today = LocalDate.of(2026, 11, 26);
System.out.println(2026 - 1990);
System.out.println(Period.between(LocalDate.of(1990, 12, 15), today).getYears());
Verified output:
36
35
The age goes up on the birthday itself, not on 1 January — one day either side proves it: the same call returns 35 on 14 May 2026 and 36 on 15 May 2026 (verified). Period.between() and ChronoUnit.YEARS.between() both get this right because they compare the month and day as well as the year; plain year subtraction never does. The same trap appears in every language, so if you have seen it once, you will recognise it everywhere.
Leap Day Birthdays: What Java Really Does on 28 February
Leap-day birthdays are the edge case most tutorials skip when they explain how to calculate age in Java. A person born on 29 February has a real birthday only in leap years, so in a common year like 2026, when do they turn a year older — 28 February or 1 March?
Here is what Java actually returns, run on JDK 21:
import java.time.LocalDate;
import java.time.Period;
LocalDate leapBirth = LocalDate.of(2000, 2, 29);
System.out.println(Period.between(leapBirth, LocalDate.of(2026, 2, 28)));
System.out.println(Period.between(leapBirth, LocalDate.of(2026, 2, 28)).getYears());
System.out.println(Period.between(leapBirth, LocalDate.of(2026, 3, 1)));
System.out.println(Period.between(leapBirth, LocalDate.of(2026, 3, 1)).getYears());
Verified output:
P25Y11M30D
25
P26Y1D
26
So Period.between() counts the next year complete on 1 March in a non-leap year, because 28 February is one day short of the 29th: still 25 on 28 February 2026, 26 on 1 March 2026.
That is not the rule this site uses. Our leap year birthday guide follows the 28 February convention used in England and Wales, where a leapling turns a year older on 28 February in a non-leap year. The two rules differ by one day and neither is a bug — your code must match the rule your app promises.
Java gives you the 28 February version too, through plusYears(), which clamps 29 February to 28 February in a common year:
System.out.println(LocalDate.of(2000, 2, 29).plusYears(26));
System.out.println(LocalDate.of(2008, 2, 29).plusYears(18));
Verified output:
2026-02-28
2026-02-28
So if your app must follow the 28 February rule, do not rely on Period alone to decide how to calculate age in Java for a leapling — add the plusYears() check. A leapling born 29 February 2008 reaches 18 on 28 February 2026 under the 28 February rule, but Period alone would still say 17 on that date — so write a test for the exact date and the rule your users expect.
The Legacy Calendar Trap
Old tutorials still teach the Calendar version of how to calculate age in Java, and it is the one most likely to be wrong — the naive version subtracts the years and never checks the month and day:
import java.util.Calendar;
Calendar birth = Calendar.getInstance();
birth.set(1990, Calendar.DECEMBER, 15);
Calendar todayCal = Calendar.getInstance();
todayCal.set(2026, Calendar.NOVEMBER, 26);
int age = todayCal.get(Calendar.YEAR) - birth.get(Calendar.YEAR);
System.out.println(age);
Verified output:
36
That is wrong: the person born 15 December 1990 is 35 on 26 November 2026, as the year-subtraction section proved. A manual month-and-day check fixes it — the same program then prints 35 (verified) — but that is exactly the hand-written date logic Period.between() exists to replace, where every branch is another chance for an off-by-one error. Calendar adds its own traps: months are 0-based, so Calendar.DECEMBER is 11, and the class is mutable and not thread-safe. The older Date.getYear() is worse: it is deprecated and returns the year minus 1900, so 2026 comes back as 126 — subtracting those values gives 36 for the December birthday too (verified), the same wrong answer.
For new code, use java.time. For old code that hands you a java.util.Date, convert it once to LocalDate and use Period from there — do not write new Calendar arithmetic.
Reading a Birth Date from Text
Birth dates usually arrive as text — from a form, a CSV file or JSON. LocalDate.parse() reads ISO yyyy-MM-dd directly, and a DateTimeFormatter reads any other pattern; both throw DateTimeParseException for bad input, so validate first.
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
LocalDate iso = LocalDate.parse("1990-05-15");
System.out.println(iso);
DateTimeFormatter ukFormat = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate fromForm = LocalDate.parse("15/05/1990", ukFormat);
System.out.println(fromForm);
Verified output:
1990-05-15
1990-05-15
Two details matter. First, the pattern letters: dd/MM/yyyy reads day first, the format most UK forms use — MM/dd/yyyy would fail on the same text. Second, always reject a birth date in the future before you calculate: a future birth date gives a negative Period, and a negative age is a bug in every app. Throw an IllegalArgumentException, as the complete class below does.
Which Method Should You Use?
The table below sums up how to calculate age in Java with each method (same example: born 15 May 1990, on 26 November 2026).
| Need | Code | Result |
|---|---|---|
| Age as people say it | Period.between(b, t).getYears() | 36 |
| Age with months and days | Period.between(b, t) | 36 years, 6 months, 11 days |
| Age as one number of years | ChronoUnit.YEARS.between(b, t) | 36 |
| Age in total months | ChronoUnit.MONTHS.between(b, t) | 438 |
| Age in total weeks | ChronoUnit.WEEKS.between(b, t) | 1906 |
| Age in total days | ChronoUnit.DAYS.between(b, t) | 13344 |
| Age from a text date | LocalDate.parse(text) then Period | 36 |
| Legacy code only | Calendar with month/day correction | 35 (corrected) |
Default to Period.between() — the only method giving the full breakdown, agreeing with ChronoUnit on whole years — and reach for ChronoUnit for a single total. Never use year subtraction, Date.getYear(), or dividing milliseconds by 365.25 days: the average-year division is wrong on many birthdays.
The same comparison exists in the other languages on this site: the how to calculate age in Python guide uses relativedelta, the how to calculate age in JavaScript guide shows the 0-indexed-month trap, and the how to calculate age in SQL guide covers the same Feb 29 split. The birth dates are the same in all four, so the answers match.
A Complete Copy-Paste AgeCalculator Class
Here is everything above in one class you can drop into a project — the finished version of how to calculate age in Java from this guide, with input validation and the full breakdown.
import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
public class AgeCalculator {
public static int calculateAge(LocalDate birthDate, LocalDate today) {
if (birthDate.isAfter(today)) {
throw new IllegalArgumentException("Birth date is in the future: " + birthDate);
}
return Period.between(birthDate, today).getYears();
}
public static String detailedAge(LocalDate birthDate, LocalDate today) {
Period age = Period.between(birthDate, today);
return age.getYears() + " years, " + age.getMonths() + " months, " + age.getDays() + " days";
}
public static void main(String[] args) {
LocalDate today = LocalDate.of(2026, 11, 26);
LocalDate birthDate = LocalDate.parse("1990-05-15", DateTimeFormatter.ISO_LOCAL_DATE);
System.out.println("Age: " + calculateAge(birthDate, today));
System.out.println("Detailed: " + detailedAge(birthDate, today));
System.out.println("Leapling: " + detailedAge(LocalDate.of(2000, 2, 29), today));
}
}
Verified output (compiled and run on OpenJDK 21.0.13.1):
Age: 36
Detailed: 36 years, 6 months, 11 days
Leapling: 26 years, 8 months, 28 days
In production, replace the fixed today with LocalDate.now() in the user’s zone and keep the future-date check — it is the one validation every age method needs. For the full reference, see Oracle’s Java tutorial on Period and Duration.
Conclusion
Once you have run the examples, you know how to calculate age in Java the way production code does it: Period.between() for the age and the full breakdown, ChronoUnit for a single total, and DateTimeFormatter for text dates. Leave behind year subtraction, uncorrected Calendar, and dividing by 365.25 — and for leap-day birthdays remember Java counts the next year complete on 1 March, so add the plusYears() check if your app promises 28 February.
What is the easiest way to work out how to calculate age in Java?
Use `Period.between(birthDate, today).getYears()`. It needs only `java.time.LocalDate` and `java.time.Period`, it is built into Java 8 and later, and it handles birthdays that have not happened yet this year. For the full breakdown, read `getMonths()` and `getDays()` from the same `Period`.
What is the difference between Period.between() and ChronoUnit.YEARS.between()?
For whole years they return the same number — 36 here. The difference is what else they give you: `Period` also returns the months and days since the last birthday, while `ChronoUnit` returns a single total, such as 438 months or 13,344 days.
How do I calculate age in Java from a date of birth typed as text?
Parse it first with `LocalDate.parse()`: ISO `1990-05-15` parses directly, and for a UK form date such as `15/05/1990` use `DateTimeFormatter.ofPattern(“dd/MM/yyyy”)`. Then pass the parsed `LocalDate` to `Period.between()`, rejecting bad text and future birth dates first.
Why does subtracting the birth year from the current year give the wrong age?
Because it ignores the month and day: `2026 – 1990` gives 36 for everyone born in 1990, but someone born on 15 December 1990 is still 35 on 26 November 2026. `Period.between()` compares the month and day as well, so the age goes up on the birthday itself.
How does Java handle someone born on 29 February?
`Period.between()` counts the next year as complete on 1 March in a non-leap year: born 29 February 2000, a person is still 25 on 28 February 2026 and turns 26 on 1 March 2026. For the 28 February convention used in England and Wales, use `birthDate.plusYears(n)` instead — it clamps 29 February to 28 February in a common year.
Should I still use Calendar or Date to calculate age in Java?
No, not for new code. `Calendar` needs a hand-written month-and-day correction, its months are 0-based, and it is not thread-safe. `Date.getYear()` is deprecated and returns the year minus 1900. Convert any legacy `Date` to `LocalDate` once, then use `Period.between()`.