Java 생일로 나이 구하기

public static int getAgeFromBirthday(Date birthday) {
    Calendar birth = new GregorianCalendar();
    Calendar today = new GregorianCalendar();

    birth.setTime(birthday);
    today.setTime(new Date());

    int factor = 0;
    if (today.get(Calendar.DAY_OF_YEAR) < birth.get(Calendar.DAY_OF_YEAR)) {
        factor = -1;
    }
    return today.get(Calendar.YEAR) - birth.get(Calendar.YEAR) + factor;
}
Comments