Calculate Age from Date of Birth
Given a date of birth and a reference date, return the age in completed years.
Subtract the birth year from the current year, then subtract one more if the birthday has not happened yet this year. The birthday is still ahead when the current month is before the birth month, or the months are equal and the current day is before the birth day.
Problem Statement
Given a person's date of birth (day, month, year) and a reference "today" date, compute their age in completed years.
Start from the difference in years, but subtract one if the person has not yet
had their birthday in the reference year. For example, someone born on
2000-05-20, measured on 2026-07-16, is 26; someone born on 2000-08-10,
measured on the same day, is still 25 because their birthday has not arrived
yet this year.
Input: Six integers: birth day, birth month, birth year, and today's day, month, year.
Output: A single integer: the age in completed years.
Examples
Input: dob=2000-05-20, today=2026-07-16
Output: 26The May birthday already passed in 2026, so the age is exactly 2026 - 2000 = 26.
Input: dob=2000-08-10, today=2026-07-16
Output: 25The August birthday has not arrived by July 16, so subtract one: 26 - 1 = 25.
Constraints
The date of birth is on or before the reference dateBoth dates are valid Gregorian dates
Think Before You Code
Reveal the questions to ask yourself first
- What is the first, rough estimate of age before any adjustment?
- Under what month/day conditions has the birthday not yet happened this year?
- Why must you compare the day only when the two months are equal?
Hints
Open them one at a time — try after each before revealing the next.
Hint 1
Hint 2
Hint 3
Approach
Reveal the step-by-step approach
Compute a year difference, then correct for the birthday.
- Set
age = todayYear - birthYear. - Decide whether the birthday has already occurred this year:
- It has NOT occurred if
todayMonth < birthMonth. - It has NOT occurred if
todayMonth == birthMonth && todayDay < birthDay.
- It has NOT occurred if
- If the birthday has not occurred yet, subtract 1 from
age. - Return
age.
The day comparison only matters when the months are equal; in every other month case the month comparison alone decides whether the birthday has passed.
Dry Run
Walk through the example step by step
Age for dob = 2000-08-10 measured on today = 2026-07-16:
age = todayYear - birthYear = 2026 - 2000 = 26
todayMonth(7) < birthMonth(8) ? yes -> birthday not yet reached
subtract 1 -> age = 25
result: 25
Solution
Reveal the full Java solution
public class AgeCalculator {
public static int calculateAge(int birthDay, int birthMonth, int birthYear,
int today, int todayMonth, int todayYear) {
int age = todayYear - birthYear;
boolean birthdayNotYet =
(todayMonth < birthMonth) ||
(todayMonth == birthMonth && today < birthDay);
if (birthdayNotYet) {
age -= 1;
}
return age;
}
public static void main(String[] args) {
// dob 2000-05-20, today 2026-07-16
System.out.println(calculateAge(20, 5, 2000, 16, 7, 2026)); // 26
// dob 2000-08-10, today 2026-07-16
System.out.println(calculateAge(10, 8, 2000, 16, 7, 2026)); // 25
}
}
The year difference is only correct once the person has had their birthday in
the current year; before that, they are still one year younger than the raw
subtraction suggests. The birthdayNotYet flag captures that "before the
birthday" condition precisely, checking the day only when the months tie.
Because the age is measured in whole years, no rounding or fractional handling is needed — a single conditional subtraction is enough.
O(1)Space: O(1)Common Mistakes
- Returning todayYear - birthYear without adjusting for a birthday that has not happened yet.
- Comparing days regardless of month, instead of only when the months are equal.
Edge Cases to Test
- Reference date exactly on the birthday counts the full year (not subtracted).
- A birthday later in the same month as today must still subtract a year.
- A person born this year (age 0) before their first birthday returns 0.
Interview Follow-Ups
- How would you also report the age in months and days, not just years?
- How would you handle a February 29 birthday in a non-leap reference year?
Practising for Java interviews?
CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.
Explore the Java Full Stack program →