Important alert: (current site time 11/23/2014 6:34:45 AM EDT)
 

VB icon

Age Calculator

Email
Submitted on: 3/26/2002 7:08:13 PM
By: Thomas Courtney 
Level: Beginner
User Rating: By 3 Users
Compatibility: Java (JDK 1.1), Java (JDK 1.2)
Views: 64470
(About the author)
 
     This code takes the user birthday and calculates the number of years, months and days since the user was born.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Age Calculator
// Description:This code takes the user birthday and calculates the number of years, months and days since the user was born.
// By: Thomas Courtney
//
// Inputs:The inputs are Year the user was born, month and day. The input cannot go out of the required paramaters.
//
// Returns:returns the number of years, months, and days from the current date to the user birthday.
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=2778&lngWId=2//for details.//**************************************

import java.util.*;
public class age
{
		
	public static void main(String args[])throws Exception
	{
	
	int year, month, day, outYear,outDay, outMonth, userYr,userMth,userDay;
	String usrYear = new String();
	String usrMonth = new String();
	String usrDay = new String();
	String input = new String();
	GregorianCalendar date = new GregorianCalendar();
	year = date.get(Calendar.YEAR);
	month = date.get(Calendar.MONTH);
	day = date.get(Calendar.DATE);
	
	System.out.print("Enter the year you were born: ");
	usrYear = getYear(year);
	userYr = Integer.parseInt(usrYear);
	
	System.out.print("Enter the month you were born: ");
	usrMonth = getMonth();
	userMth = Integer.parseInt(usrMonth);
	
	System.out.print("Enter the day you were born: ");
	usrDay = getDay(userMth);
	userDay = Integer.parseInt(usrDay);
	
	System.out.println("Your birthday is on " + userMth + "/" + userDay + "/" + userYr);
	displayAge(userYr, year, userMth, month, userDay, day);
	}
	
	
	public static String getYear(int yr)throws Exception
	{	
		int outYear=0;
		int year;
		year = yr;
		String usrYear = new String();
		String output = new String();
		usrYear = getString();
		
		try
		{		
			outYear = Integer.parseInt(usrYear);
			if(outYear < 1800 || outYear > year)
			throw(new NumberFormatException());
		}
		
		catch(NumberFormatException error)
		{
			outYear = check(("Input must be between 1800-" + year + ": "),1);
		}
		
		output = Integer.toString(outYear);
		return output;
	}
		
	public static String getMonth()throws Exception
	{	
		int outMonth=0;
		String usrMonth = new String();
		String output = new String();
		usrMonth = getString();
		try
		{		
			outMonth = Integer.parseInt(usrMonth);
			if(outMonth < 1 || outMonth > 12)
			throw(new NumberFormatException());
		}
		
		catch(NumberFormatException error)
		{
			outMonth = check(("Input must be between 1-12: "),2);
		}
		output = Integer.toString(outMonth);
		return output;
	}	
	
	public static String getDay(int mth)throws Exception
	{	
		int outDay=0;
		int [] dayInMonth = {31,28,31,30,31,30,31,31,30,31,30,31};
		int usrMth, dayInMth;
		usrMth=mth;
		dayInMth = dayInMonth[usrMth - 1];
		String usrDay = new String();
		String output = new String();
		usrDay = getString();
		try
		{		
			outDay = Integer.parseInt(usrDay);
			if(outDay < 1 || outDay > dayInMth)
			throw(new NumberFormatException());
		}
		
		catch(NumberFormatException error)
		{
			outDay = check(("Input must be between 1-" + dayInMth + ": "),3);
		}
		output = Integer.toString(outDay);
		return output;
	}
	
	public static void displayAge(int yr, int tdYr, int mth, int tdMth, int usday, int tdDay)
	{
		int usrYear, year, usrMonth, month, usrDay, day, outDay=0, outMonth, outYear, tempMth;
		String pyr = new String();
		String pmth = new String();
		String pdy = new String();
		int [] dayInMonth = {31,28,31,30,31,30,31,31,30,31,30,31};
		int tempDyMth; 
		pyr = "year";
		pmth = "month";
		pdy = "day";
		usrYear = yr;
		year = tdYr;
		usrMonth = mth;
		month = tdMth;
		usrDay = usday;
		day = tdDay;
		outDay = day - usrDay;
		tempMth = usrMonth;
		outMonth = (month+1) - usrMonth;
		outYear = year - usrYear;
		
			if (usrMonth-1<month&&usrDay>day)
			{
				outMonth = outMonth - 1;
				tempDyMth = dayInMonth[usrMonth-1];
				outDay = tempDyMth+outDay;
			
			}
			if ( outDay<0 )
			{
			outDay = getDaysInMonth(tempMth,usrDay,day);
			outMonth = Math.abs(outMonth);
			outMonth=(12-outMonth)-1;
			outYear=outYear-1;
			}
			
		if (outMonth<0)
		{	
			outMonth=Math.abs(outMonth); 
			outMonth=12-outMonth;
			outYear=outYear-1;
		}
		if( outYear > 1 )
			pyr = "years";
		if( outMonth > 1 )
			pmth = "months";
		if( outDay > 1 )
			pdy = "days";
		
		System.out.println("As of today (" + (month+1) +"/"+day+"/"+year+") you are "
			 + outYear + " " + pyr + " "
			+ outMonth + " " + pmth + " "
			+ outDay + " " + pdy + " old.");
		
	}
		 
	public static int getDaysInMonth(int usrMth, int usrDy, int tdDay)
	{
		int usrMonth, usrDay, daysInMth,day, outDay;
		int [] dayInMonth = {31,28,31,30,31,30,31,31,30,31,30,31};
		usrMonth = usrMth-1;
		usrDay = usrDy;
		day = tdDay;
	
			daysInMth = dayInMonth[usrMonth];
			outDay = daysInMth - usrDay;
			outDay = outDay + day;
		
			
		return outDay;
	}
		
			
 	public static String getString() throws Exception
	{ 
 	 char newchar;
	
 	 String newstring = new String();
 newchar = (char)System.in.read();
 while (newchar != '\r')
 	{
 		 newstring = newstring + newchar;
 		 newchar = (char)System.in.read();
	 	}
 	System.in.read(); 
	return newstring;
 	} 
	
	public static int check(String err,int n)throws Exception
	{
		boolean test = false;
		String testStr = new String();
		int output = 0;
		while (test == false) 
		{
			System.out.print(err);
			testStr = getString();
			test = testIn(testStr,n);
		}
		output = Integer.parseInt(testStr);
		return output;
}
 	public static boolean testIn(String str,int x)
	{
		int input = 0;
	 	boolean test = true;
	try
	{
	 	input = Integer.parseInt(str);
	}
	catch (NumberFormatException one)
	{
		test = false;
	}
	switch (x)
	{
	case 1: 
	 if (test == true && input > 2002)
	 	test = false;
	 if (test == true && input < 1800)
	 	test = false;
	 break;
	case 2: 
	 if (test == true && input < 1)
	test = false;
	 if (test == true && input > 12)
	test= false;
	 break;
	case 3: 
	 	 if (test == true && input < 1)
		 test = false;
		 if (test == true && input >= 31)
		 test = false;
		}
	return test;
	 }
}
	
	


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

1/21/2007 4:43:02 AM

good

(If this comment was disrespectful, please report it.)

 
12/22/2007 5:28:09 AM

Need help, when trying this code i am getting a Exception
(If this comment was disrespectful, please report it.)

 
1/3/2008 8:30:53 AMSirfraz ahmed

verry good sir
(If this comment was disrespectful, please report it.)

 
5/31/2009 12:56:38 PMtonio09

bloated and incorrect :/
what about leap years?
(If this comment was disrespectful, please report it.)

 
10/17/2012 12:35:25 AMKhalil Basha

Not good..leap year calcualation fails..recheck it
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.