Java Age Calculate calculateAge(Date dob)

Here you can find the source of calculateAge(Date dob)

Description

calculate Age

License

Apache License

Declaration

public static Integer calculateAge(Date dob) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Main {
    public static Integer calculateAge(Date dob) {
        Calendar cal = new GregorianCalendar();
        cal.setTime(dob);//from   w  ww.  j  a  va  2s.co  m
        Calendar now = new GregorianCalendar();
        int res = now.get(Calendar.YEAR) - cal.get(Calendar.YEAR);
        if ((cal.get(Calendar.MONTH) > now.get(Calendar.MONTH))
                || (cal.get(Calendar.MONTH) == now.get(Calendar.MONTH)
                        && cal.get(Calendar.DAY_OF_MONTH) > now.get(Calendar.DAY_OF_MONTH))) {
            res--;
        }
        return res;
    }
}

Related

  1. calculateAge(Date aDateFrom, Date aDateTo)
  2. calculateAge(Date birthDate)
  3. calculateAge(Date birthday)
  4. calculateAge(Date dateOfBirth)
  5. calculateAge(Date DOB)
  6. calculateAgeInMonth(final Date birthDate, final Date asOfDate)
  7. calculateAgeInWeek(final Date birthDate, final Date asOfDate)
  8. getAge(Date birthDate, Date controlDate)
  9. getAge(Date birthday)