Home Java Example Java Util Determining the actual age from date of birth in Java
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Determining the actual age from date of birth in Java

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

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.

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 16, 2007

Ask Questions?    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
Adilson Victor
April 18, 2011
Very Good!

Finally I've found something useful in the internet about dates in java....Thanks for this code.
karthikeyan
December 27, 2011
Errors

working for some inputs. showing errors for some inputs ex., 1988,10,28 - shows days in negative
M Asif Iqbal
January 10, 2012
Bug in the age method

There is a bug in the above method. Check with this method. "1953-09-07"
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
DMCA.com