Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

birthdate class

I need to know how to prompt the user for their birth date and calculate and display how many days old they are. Can anyone help me out, because i am completely stuck, thanks in advance.
[186 byte] By [oribit] at [2007-11-11 10:00:28]
# 1 Re: birthdate class
Do you want user input from the console or from a dialog window?
Phaelax at 2007-11-11 22:32:06 >
# 2 Re: birthdate class
user input from a dialog window would be best.
oribit at 2007-11-11 22:33:06 >
# 3 Re: birthdate class
Use the methods of the Date and Calendar classes to represent the birthdate, today, and the difference between the two.
nspils at 2007-11-11 22:34:10 >
# 4 Re: birthdate class
import java.util.Calendar;
import java.util.GregorianCalendar;

public class AgeCalculator {

public static void main(String[] args) {

int dob_date = 7;
int dob_month = 7;
int dob_year = 1979;

// Create a calendar object with the DOB
Calendar dob = new GregorianCalendar(dob_year, dob_month, dob_date);

// Create a calendar object with today's date
Calendar today = Calendar.getInstance();

int diff_year = (today.get(Calendar.YEAR) - dob.get(Calendar.YEAR));
int diff_month = (today.get(Calendar.MONTH) + 1) - dob.get(Calendar.MONTH);
int diff_date = (today.get(Calendar.DATE) - dob.get(Calendar.DATE));

int age = 0;

if(diff_year > 0){

age = diff_year;

if(diff_month < 0) age = age - 1;

else if(diff_month == 0){

if(diff_date < 0) age = age - 1;
}
}
System.out.println("Age : " + age);
}
}
fenoyf at 2007-11-11 22:35:04 >