Determining a Person's Age

This example uses the Calendar class to compute a person's age.
// Create a calendar object with the date of birth Calendar dateOfBirth = new GregorianCalendar(1972, Calendar.JANUARY, 27); // Create a calendar object with today's date Calendar today = Calendar.getInstance(); // Get age based on year int age = today.get(Calendar.YEAR) - dateOfBirth.get(Calendar.YEAR); // Add the tentative age to the date of birth to get this year's birthday dateOfBirth.add(Calendar.YEAR, age); // If this year's birthday has not happened yet, subtract one from age if (today.before(dateOfBirth)) { age--; }

Comments

9 Feb 2010 - 3:32am by Anonymous (not verified)

tnx . . .its easy 2 understand

16 Feb 2010 - 7:04pm by Ganesh (not verified)

Its useful for me to understand collections

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.