- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find the age when date of birth is known? Using Java?
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.
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
- Related Articles
- How to convert birth date records to age with MongoDB
- Calculate age from date of birth in MySQL?
- Calculate age based on date of birth in MySQL?
- Calculate Age from given Date of Birth in MySQL?
- How can we find the employees from MySQL table whose age is greater than say 30 years, providing the only date of birth on the table?
- How to calculate retirement date from date of birth in Excel?
- Java program to display Astrological sign or Zodiac sign for given date of birth
- How can I find the percentage of my users whose birth date is between 1980 and 1996 in MySQL?
- How to display the day name on the basis of Date of Birth records in MySQL?
- Present age of father is 42 years and that of his son is 14 years. Find the ratio of(a) Present age of father to the present age of son.(b) Age of the father to the age of son, when son was 12 years old.(c) Age of father after 10 years to the age of son after 10 years.(d) Age of farher to the age of son when father was 30 years old.
- MySQL query to get the dates between range of records displaying student’s Date of Birth?
- How to find the first date of a given year using Python?
- The sum of three times the age of the son and the age of the father is 110. If three times the age of his father is added to the age of son we get 170. Then what is the sum of their age? Find their ages.
- How to calculate average age by year/month/date in Excel?
- How to Auto-Sort Date When Date is Entered or Changed in Excel?
