Skip to content

Instantly share code, notes, and snippets.

@timendum
Created July 27, 2011 10:13
Show Gist options
  • Select an option

  • Save timendum/1109077 to your computer and use it in GitHub Desktop.

Select an option

Save timendum/1109077 to your computer and use it in GitHub Desktop.
Calculate the age from birth date
/**
* Calculate the age from birth date
* @param birth the birth date
* @return the age
*/
public static int getAge(Calendar birth) {
Calendar today = Calendar.getInstance();
int age = today.get(Calendar.YEAR) - birth.get(GregorianCalendar.YEAR);
if (
today.get(Calendar.MONTH) < birth.get(GregorianCalendar.MONTH) ||
(
today.get(Calendar.MONTH) == birth.get(GregorianCalendar.MONTH) &&
today.get(Calendar.DAY_OF_MONTH) < birth.get(GregorianCalendar.DAY_OF_MONTH)
)
) {
age--;
}
return age;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment