Home Java Example Java Util Determining the actual age from date of birth in Java



Determining the actual age from date of birth in Java
Posted on: April 16, 2007 at 12:00 AM
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.

Related Tags for Determining the actual age from date of birth in Java:
ccomidedateiovirmithisidbirtexampleprogramramexamearbiemonthdessectionulpecompleteinrmcalcalcmntmincacalculateminletesageprodayyearxawhichxampstermsodaysatanylatekisirhaivmpleaandaractrtxactssthhatetermetermpleplprmindonomogro


More Tutorials from this section

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
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.