Calculate age from BirthDate

2020-06-03 01:25发布

I have DatePicker Dialog, When I select date at that time I want to calculate age it's working but when I select date of current year at that time it showing the -1 age instead of 0 then how can solve this? Please help me to solve it. My code is below:

public int getAge(int year, int month, int day) {

        GregorianCalendar cal = new GregorianCalendar();
        int y, m, d, noofyears;

        y = cal.get(Calendar.YEAR);// current year ,
        m = cal.get(Calendar.MONTH);// current month
        d = cal.get(Calendar.DAY_OF_MONTH);// current day
        cal.set(year, month, day);// here ur date
        noofyears = (int) (y - cal.get(Calendar.YEAR));
        LOGD("Age......", String.valueOf(noofyears));

        if ((m < cal.get(Calendar.MONTH)) || ((m == cal.get(Calendar.MONTH)) && (d < cal.get(Calendar.DAY_OF_MONTH)))) {
            --noofyears;
        }
        LOGD("Age......", String.valueOf(noofyears));
        if (noofyears != 0) {
            ageCount = noofyears;
        } else {
            ageCount = 0;
        }
        if (noofyears < 0)
            throw new IllegalArgumentException("age < 0");
        return noofyears;
    }

15条回答
beautiful°
2楼-- · 2020-06-03 02:08
 private boolean getAge(int year, int month, int day) {
    try {
        Calendar dob = Calendar.getInstance();
        Calendar today = Calendar.getInstance();
        dob.set(year, month, day);
        int monthToday = today.get(Calendar.MONTH) + 1;
        int monthDOB = dob.get(Calendar.MONTH)+1;
        int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);
        if (age > 18) {
            return true;
        } else if (age == 18) {
            if (monthDOB > monthToday) {
                return true;
            } else if (monthDOB == monthToday) {
                int todayDate = today.get(Calendar.DAY_OF_MONTH);
                int dobDate = dob.get(Calendar.DAY_OF_MONTH);
                if (dobDate <= todayDate) { // should be less then
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            return false;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}
查看更多
别忘想泡老子
3楼-- · 2020-06-03 02:11

This is how I implement in my source code, I tested. Hope that it is useful :

public static int getAge(String dateTime, String currentFormat) {
    SimpleDateFormat dateFormat = new SimpleDateFormat(currentFormat);

    try {
        Date date = dateFormat.parse(dateTime);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);


        Date currentDate = new Date();
        Calendar currentCalendar = Calendar.getInstance();
        currentCalendar.setTime(currentDate);

        int currentYear = currentCalendar.get(Calendar.YEAR);
        int currentMonth = currentCalendar.get(Calendar.MONTH);
        int currentDay = currentCalendar.get(Calendar.DAY_OF_MONTH);

        int deltaYear = currentYear - year;
        int deltaMonth = currentMonth - month;
        int deltaDay = currentDay - day;

        if (deltaYear > 0) {
            if (deltaMonth < 0) {
                deltaYear --;
            } else if (deltaDay < 0){
                deltaYear --;
            }

            return deltaYear;
        }
    } catch (java.text.ParseException e) {
        e.printStackTrace();
    }

    return 0;
}
查看更多
forever°为你锁心
4楼-- · 2020-06-03 02:13

Now for kotlin Language:

import java.util.Calendar


fun main(args: Array<String>) {


print(getAge(yyyy,mm,dd))

}

 fun getAge(year: Int, month: Int, day: Int): String {
    val dob = Calendar.getInstance()
    val today = Calendar.getInstance()

    dob.set(year, month, day)

    var age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR)

    if (today.get(Calendar.DAY_OF_YEAR) < dob.get(Calendar.DAY_OF_YEAR)) {
        age--
    }

    val ageInt = age + 1

    return ageInt.toString()
}
查看更多
登录 后发表回答