How to find the age when date of birth is known? Using Java?

Java 8Object Oriented ProgrammingProgramming

Java provides a class named Period in the java.time package. This is used to calculate the time period between two given dates as days, months and, years etc.

The between() method of this class accepts two LocalDate objects and finds out the period between the two given dates (number of years, months, and days) and returns as a period object.

From this, you can extract the number of days, months and, years of the period in between using the getDays(), getMonths() and, getYears() respectively.

Finding the age

If you already have the date of birth of a person, to find the age −

  • Get the date of birth from the user.
  • Convert it to LocalDate object.
  • Get the current date (as LocalDate object)
  • Find the period between these two dates using the between() method as −
Period period = Period.between(dateOfBirth, LocalDate.now());
  • Get the days, months, years from the Period object using the getDays(), getMonths() and, getYears() methods as −

period.getYears();
period.getMonths();
period.getDays();

Example

Following example reads the name and date of birth from the user, converts it to LocalDate object, gets the current date, finds out the period between these two dates and prints it as days, months and years.

 Live Demo

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.Scanner;
public class CalculatingAge {
   public static Date StringToDate(String dob) throws ParseException{
      //Instantiating the SimpleDateFormat class
      SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
      //Parsing the given String to Date object
      Date date = formatter.parse(dob);
      System.out.println("Date object value: "+date);
      return date;
   }
   public static void main(String args[]) throws ParseException {
      //Reading name and date of birth from the user
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter your name: ");
      String name = sc.next();
      System.out.println("Enter your date of birth (dd-MM-yyyy): ");
      String dob = sc.next();
      //Converting String to Date
      SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
      Date date = formatter.parse(dob);
      //Converting obtained Date object to LocalDate object
      Instant instant = date.toInstant();
      ZonedDateTime zone = instant.atZone(ZoneId.systemDefault());
      LocalDate givenDate = zone.toLocalDate();
      //Calculating the difference between given date to current date.
      Period period = Period.between(givenDate, LocalDate.now());
      System.out.print("Hello "+name+" your current age is: ");
      System.out.print(period.getYears()+" years "+period.getMonths()+" and "+period.getDays()+" days");
   }
}

Output

Enter your name:
Krishna
Enter your date of birth (dd-MM-yyyy):
26-07-1989
Hello Krishna your current age is: 29 years 10 and 5 days
raja
Published on 04-Jul-2019 14:34:43
Advertisements