|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return targetDate; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static String getAgeForBirthDay(Date birthDay) { | 
|---|
|  |  |  | Calendar cal = Calendar.getInstance(); | 
|---|
|  |  |  | if (birthDay == null) { | 
|---|
|  |  |  | return "-"; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | // 出生日期晚于当前时间,无法计算 | 
|---|
|  |  |  | if (cal.before(birthDay)) { | 
|---|
|  |  |  | return "-"; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | // 当前年份 | 
|---|
|  |  |  | int yearNow = cal.get(Calendar.YEAR); | 
|---|
|  |  |  | // 当前月份 | 
|---|
|  |  |  | int monthNow = cal.get(Calendar.MONTH); | 
|---|
|  |  |  | // 当前日期 | 
|---|
|  |  |  | int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH); | 
|---|
|  |  |  | cal.setTime(birthDay); | 
|---|
|  |  |  | int yearBirth = cal.get(Calendar.YEAR); | 
|---|
|  |  |  | int monthBirth = cal.get(Calendar.MONTH); | 
|---|
|  |  |  | int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH); | 
|---|
|  |  |  | // 计算整岁数 | 
|---|
|  |  |  | Integer age = yearNow - yearBirth; | 
|---|
|  |  |  | if (monthNow <= monthBirth) { | 
|---|
|  |  |  | if (monthNow == monthBirth) { | 
|---|
|  |  |  | if (dayOfMonthNow < dayOfMonthBirth) { | 
|---|
|  |  |  | // 当前日期在生日之前,年龄减一 | 
|---|
|  |  |  | age--; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | age--; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return age.toString(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|