Calculate Age from Date of Birth in Java

Learn how to Calculate Age from date of birth in Java using LocalDate , Period, and DateTimeFormatter. A LocalDate represents a date without time and is part of the Java Time API introduced in Java. Also The Period class calculates the difference between two dates in years, months, and days.

Code Explanation (Step-by-Step)

  • Create the isValidAge() method that accepts the date of birth as a String.
  • Use DateTimeFormatter.ofPattern(“dd/MM/yyyy”) to define the date format.
  • Convert the date of birth string into a LocalDate object using the parse() method.
  • Use Period.between(userDob, LocalDate.now()) to calculate the difference between the date of birth and the current date.
  • Call the getYears() method to retrieve the age in years.
  • Display the calculated age.
  • Use an if condition to check whether the age is between 18 and 99 years.
  • If the age is valid, return true and display a valid age message.
  • Otherwise, return false and display an invalid age message.
  • Call the isValidAge() method from the main() method and print the result.
				
					import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;

public class CheckValidAge {

    public static void main(String[] args) {
        boolean s = isValiAge("03/10/1984");
        System.out.println("User age is valid: " + s);
    }

    public static boolean isValiAge(String dob) {

        boolean result = false;
        StringBuilder sb = new StringBuilder();

        LocalDate userDob = LocalDate.parse(
                dob,
                DateTimeFormatter.ofPattern("dd/MM/yyyy"));

        int age = Period.between(userDob, LocalDate.now()).getYears();

        System.out.println("User age is: " + age + " years");

        if (age >= 18 && age <= 99) {
            sb.append("User age is between 18 and 99 years old");
            result = true;
        } else {
            sb.append("User age is not between 18 and 99 years old");
            result = false;
        }

        System.out.println(sb);
        return result;
    }
}
				
			

Output :-
User age is:- 38 years
User age is between 18 and 99 years old
User age is valid:- true

Java Program to Calculate Age from Date of Birth Using ChronoUnit
				
					import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

public class AgeCalculator {

    public static boolean isValidAge(String dob) {

        LocalDate birthDate = LocalDate.parse(
                dob,
                DateTimeFormatter.ofPattern("dd/MM/yyyy"));

        long age = ChronoUnit.YEARS.between(
                birthDate,
                LocalDate.now());

        System.out.println("User age is: " + age + " years");

        return age >= 18 && age <= 99;
    }

    public static void main(String[] args) {

        boolean result = isValidAge("03/10/1984");

        System.out.println("User age is valid: " + result);
    }
}