Java Day From getAge(Date birthDay)

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

Description

get Age

License

Apache License

Declaration

public static int getAge(Date birthDay) throws Exception 

Method Source Code

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

import java.util.*;

public class Main {
    public static int getAge(Date birthDay) throws Exception {
        Calendar cal = Calendar.getInstance();

        if (cal.before(birthDay)) {
            throw new IllegalArgumentException("The birthDay is before Now.It's unbelievable!");
        }//w  w  w . j  a  va2  s. c om
        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) {
                if (dayOfMonthNow < dayOfMonthBirth)
                    age--;
            } else {
                age--;
            }
        }
        return age;
    }
}

Related

  1. distanceToNowInDaysIgnoreTime(Date date)
  2. equalsDay(final Date date1, final Date date2)
  3. explDay(Date date)
  4. firstDayOfCentury(final int century)
  5. get23HourOfDay(Date date)
  6. getBeforeDate_(Date date, int day)
  7. getDateByDay(Date date)
  8. getDateByNum(int day)
  9. getDateFromDayNum(int dayNum)