Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials
  

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
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.

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.

                         

Facing Programming Problem?
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

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.

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

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

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

Copyright © 2008. All rights reserved.