Java Age Calculate getAge1(Date birthDay)

Here you can find the source of getAge1(Date birthDay)

Description

get Age

License

Apache License

Declaration

public static String getAge1(Date birthDay) throws Exception 

Method Source Code

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

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

public class Main {
    public static String getAge1(Date birthDay) throws Exception {
        if (null == birthDay)
            return "-";
        Calendar cal = Calendar.getInstance();
        if (cal.before(birthDay)) {
            throw new IllegalArgumentException("The birthDay is before Now.It's unbelievable!");
        }// w  ww .  j  a v  a 2  s  . com

        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);

        int age = yearNow - yearBirth;

        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                // monthNow==monthBirth
                if (dayOfMonthNow < dayOfMonthBirth) {
                    age--;
                } else {
                    // do nothing
                }
            } else {
                // monthNow>monthBirth
                age--;
            }
        } else {
            // monthNow
            // donothing
        }
        if (age == 0)
            return "0";

        return String.valueOf(age);
    }
}

Related

  1. getAge(Date date, TimeZone tz)
  2. getAge(Date dateNaissance)
  3. getAge(Date time, Date birth)
  4. getAge(final Date birthDay, final Date currDate)
  5. getAge(String year)
  6. getAgeAt(final Date birthday, final Date date)
  7. getAgeFromBirthDate(Date birth, boolean estimate)
  8. getAgeFromBirthday(Date birthday)
  9. getAgeFromBirthday(Date date)