Determining the actual age from date of birth in Java

Here, you can calculate the age of a person easily through the given program.

Ads

Tutorials   
How to read properties file in Java? Generate random numbers in Java Cloneable Interface in Java What is the flaw with the Stack class? List In Java Java Stack Example HashSet In Java Enumeration in java How to generate random number in java Iterator in java Queue in java Transient Java Keyword clone method in Java Java Queue example Logger in Java Java current date - How to get current date in Java Collection : LinkedList Example Collection : ArrayList Example Random alpha numeric string of fixed size Java List Iterator Example The Hashtable Class The BitSet class Java Vector Java Enumeration Data Structures in Java Java Transient Variable Java Date Format Example Java Properties File Example Map.Entry Java Interface Java Property File java preferences,Change the value of Preferences by user Retrieving a Preference Node Java hasNext Write Keys and Values to the Properties file in Java Get All Keys and Values of the Properties files in Java Properties file in Java Properties file in Java Determining If a Preference Node Contains a Specific Value Change the value of Preferences by user Clone method example in Java Implementing a Simple Event Notifier Determining When a Preference Node Is Added or Removed Listening for Changes to Preference Values in a Preference Node Importing Preferences Finding a Preference in a Preference Tree Retrieving the Parent and Child Nodes of a Preference Node Determining If a Preference Node Exists Removing a Preference Node Creating a Preference Node Getting the Maximum Size of a Preference Key and Value
Ads

Determining the actual age from date of birth in Java

     

Here, you can calculate the age of a person easily through the given program.

This program takes your complete date of birth (year, month and day of month) and gives you the exact age which determines the years, months and days of the calculated age of the person. Following program is fully validated. Because of this you can not enter any invalid year or month or day for your date of birth.

 

Here is the code of the program:

 

 

import java.util.*;
import java.io.*;

public class AgeCalculation{
  public static void main(String[] argsthrows IOException{
  int day = 1, month = 0, year = 1, ageYears, ageMonths, ageDays;
  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  Calendar cd = Calendar.getInstance();
  try{
  System.out.print("Enter year of your date of birth : ");
  year = Integer.parseInt(in.readLine());
  if(year > cd.get(Calendar.YEAR)){
  System.out.print("Invalid date of birth.");
  System.exit(0);
  }
  System.out.print("Enter month of your date of birth : ");
  month = Integer.parseInt(in.readLine());
  if(month < || month > 12){
  System.out.print("Please enter monthe between 1 to 12.");
  System.exit(0);
  }
  else{
  month--;
  if(year == cd.get(Calendar.YEAR)){
  if(month > cd.get(Calendar.MONTH)){
  System.out.print("Invalid month!");
  System.exit(0);
  }
  }
  }
  System.out.print("Enter day of your date of birth : ");
  day = Integer.parseInt(in.readLine());
  if(month == || month == || month == || month == || month == || 
month == 
|| month == 11){
  if(day > 31 || day < 1){
  System.out.print("Please enter day between 1 to 31.");
  System.exit(0);
  }
  }
  else if(month == || month == || month == || month == 10){
  if(day > 30 || day < 1){
  System.out.print("Please enter day between 1 to 30.");
  System.exit(0);
  }
  }
  else{
  if(new GregorianCalendar().isLeapYear(year)){
  if(day < || day > 29){
  System.out.print("Please enter day between 1 to 29.");
  System.exit(0);
  }
  }
  else if(day < || day > 28){
  System.out.print("Please enter day between 1 to 28.");
  System.exit(0);
  }
  }
  if(year == cd.get(Calendar.YEAR)){
  if(month == cd.get(Calendar.MONTH)){
  if(day > cd.get(Calendar.DAY_OF_MONTH)){
  System.out.print("Invalid date!");
  System.exit(0);
  }
  }
  }
  }
  catch(NumberFormatException ne){
  System.out.print(ne.getMessage() " is not a legal entry!");
  System.out.print("Please enter number.");
  System.exit(0);
  }
  Calendar bd = new GregorianCalendar(year, month, day);
  ageYears = cd.get(Calendar.YEAR- bd.get(Calendar.YEAR);
  if(cd.before(new GregorianCalendar(cd.get(Calendar.YEAR), month, day))){
  ageYears--;
  ageMonths = (12 (bd.get(Calendar.MONTH1)) (bd.get(Calendar.MONTH));
  if(day > cd.get(Calendar.DAY_OF_MONTH)){
  ageDays = day - cd.get(Calendar.DAY_OF_MONTH);
  }
  else if(day < cd.get(Calendar.DAY_OF_MONTH)){
  ageDays = cd.get(Calendar.DAY_OF_MONTH- day;
  }
  else{
  ageDays = 0;
  }
  }
  else if(cd.after(new GregorianCalendar(cd.get(Calendar.YEAR), month, day))){
  ageMonths = (cd.get(Calendar.MONTH(bd.get(Calendar.MONTH)));
  if(day > cd.get(Calendar.DAY_OF_MONTH))
  ageDays = day - cd.get(Calendar.DAY_OF_MONTH- day;
  else if(day < cd.get(Calendar.DAY_OF_MONTH)){
  ageDays = cd.get(Calendar.DAY_OF_MONTH- day;
  }
  else
  ageDays = 0;
  }
  else{
  ageYears = cd.get(Calendar.YEAR- bd.get(Calendar.YEAR);
  ageMonths = 0;
  ageDays = 0;
  }
  System.out.print("Age of the person : " + ageYears + " year, " + ageMonths + 
" months and " 
+ ageDays + " days.");
  }
}

Download this example.

Advertisements

Ads
Share on Google+Share on Google+

Determining the actual age from date of birth in Java

Posted on: April 16, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Determining the actual age from date of birth in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:12
zach
January 28, 2012
Error In Code

When i run the code and put in my info it always gives me the same month Ex. Year of birth: 2001 Month Of Birth: 5 Day of birth: 23 output (example not real) 11 years, 11 months and so and so days..... u use a different month, same month output...... 11 Thanks, if you can fix this :)
siddhesh
February 23, 2012
change

according to me this ageMonths = (12 - (bd.get(Calendar.MONTH) + 1)) + (bd.get(Calendar.MONTH)); should be ageMonths = (12 - (bd.get(Calendar.MONTH) + 1)) + (cd.get(Calendar.MONTH)+1); am i right????
Danny
March 27, 2012
calculate age using java

I found your example guite helpful to me,I wish i could see more examples in your website
narendra
May 10, 2012
doubt

if i give the date as jan 21,1980, it is showing days as negative value..
Surendar S
August 9, 2012
One More Example Program

import java.io.*; class AgeCalculator{ public static void main(String args[]) { InputStreamReader ins=new InputStreamReader(System.in); BufferedReader hey=new BufferedReader(ins); try{ System.out.println("Please enter your name: "); String name=hey.readLine(); System.out.println("Please enter your birth date: "); String date=hey.readLine(); System.out.println("please enter your birth month:"); String month=hey.readLine(); System.out.println("please enter your birth year:"); String year=hey.readLine(); System.out.println("please enter current year:"); String cYear=hey.readLine(); int bDate = Integer.parseInt(date); int bMonth = Integer.parseInt(month); int bYear = Integer.parseInt(year); int ccYear=Integer.parseInt(cYear); int age; age=ccYear-bYear; int totalMonth=12; int yourMonth=totalMonth-bMonth; System.out.println(" Hi " + name + " your are " + age + " years " + yourMonth + " months old "); } catch(IOException err) { System.out.println(""); } } }
X
August 14, 2012
error....

There is some mistake in the code while calculating the months.. But it happens only in some cases.... This will solve the issue.. Calendar cd = Calendar.getInstance(); Calendar bd = new GregorianCalendar(birthYear, birthMonth, birthDate); year = cd.get(Calendar.YEAR) - bd.get(Calendar.YEAR); if(cd.before(new GregorianCalendar(cd.get(Calendar.YEAR), birthMonth, birthDate))) { year--; if(birthMonth> cd.get(Calendar.MONTH)) { month=12 - birthMonth + cd.get(Calendar.MONTH); if(birthDate > cd.get(Calendar.DAY_OF_MONTH)) { month--; } } else { month = (12 - (bd.get(Calendar.MONTH) + 1)) + (bd.get(Calendar.MONTH)); } } else if(cd.after(new GregorianCalendar(cd.get(Calendar.YEAR), birthMonth, birthDate))) { month = cd.get(Calendar.MONTH) - (bd.get(Calendar.MONTH)); if(birthDate > cd.get(Calendar.DAY_OF_MONTH)) { month--; } } else { year = cd.get(Calendar.YEAR) - bd.get(Calendar.YEAR); month = 0; }
dkcorps
August 17, 2012
Another answer

import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; /** * * @author idealSol3 */ public class dateComp { public static void main(String[] args) { try { String input = "1989-08-18"; SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date now = new Date(); Date dob = (Date) format.parse(input); double age = ((now.getTime() - dob.getTime()) / (31556926)) / 1000; System.out.println("age " + age); } catch (ParseException ex) { System.err.println("Error " + ex.getMessage()); } } }
وردة الاقصى
May 10, 2013
Q

i am need explain this cod please
Smithe853
May 10, 2014
John

I really like your writing style, great info, thank you for putting up fbkbaadecaedaddf

Ads

 

Ads