Created
July 27, 2011 10:13
-
-
Save timendum/1109077 to your computer and use it in GitHub Desktop.
Calculate the age from birth date
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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