Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Determining the actual age from date of birth in Java 
 

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.

                         

» View all related tutorials
Related Tags: c string ide class reference io references size sed get vi key value field fields this id length max preferences

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

It really solved my problem of getting the right age of a person based on his date-of-birth.
thanks a lot.
Keep it up....
-Mone

Posted by Mone on Monday, 05.28.07 @ 17:05pm | #17538

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.