Thursday, March 1, 2012

Calculate age in java


Use following code to calculate the age in java. Input parameter is calendar which contains b'day year, month, day.

public int calculateAge(Calendar bcalendar) {
        Calendar today = Calendar.getInstance();
        int age = today.get(Calendar.YEAR) - bcalendar.get(Calendar.YEAR);
        if (today.get(Calendar.DAY_OF_YEAR) < bcalendar.get(Calendar.DAY_OF_YEAR)) {
            age--;
        }
        return age;
}