public int calculateAge ( Date birthday ) { int result = - 1 ; if ( birthday != null ) { GregorianCalendar todayCal = new GregorianCalendar ( ) ; todayCal . setTime ( new Date ( ) ) ; int dayOfYear = todayCal . get ( Calendar . DAY_OF_YEAR ) ; int year = todayCal . get ( Calendar . YEAR ) ; GregorianCalendar birthdayCal = new GregorianCalendar ( ) ; birthdayCal . setTime ( birthday ) ; int birthDayOfYear = birthdayCal . get ( Calendar . DAY_OF_YEAR ) ; int birthYear = birthdayCal . get ( Calendar . YEAR ) ; birthDayOfYear = processLeapYear ( todayCal , birthdayCal , birthDayOfYear ) ; result = year - birthYear ; if ( dayOfYear < birthDayOfYear ) { result -- ; } } return result ; }
Calcualte the age in years from today given a birthday .