根据出生日期,计算年龄,精确到天

来源:互联网 发布:绩效工资表软件 编辑:程序博客网 时间:2024/05/16 15:41
public static int calcAge(String birthday) {
        int iage = 0;

        if (birthday != "" || birthday != null) {
            int year = Integer.parseInt(birthday.substring(0, 4));
            int month = Integer.parseInt(birthday.substring(5, 7));
            int day = Integer.parseInt(birthday.substring(8,10));

            Calendar birthDate = new GregorianCalendar(year, month, day);
            Calendar today = Calendar.getInstance();

        if (today.get(Calendar.YEAR) > birthDate.get(Calendar.YEAR)) {
            iage = today.get(Calendar.YEAR) - birthDate.get(Calendar.YEAR) - 1;
            if (today.get(Calendar.MONTH) + 1 > birthDate .get(Calendar.MONTH)) {
                iage++;
            } else if (today.get(Calendar.MONTH) + 1 == birthDate .get(Calendar.MONTH)) {

                if (today.get(Calendar.DAY_OF_MONTH) >= birthDate .get(Calendar.DAY_OF_MONTH)) {
                    iage++;
                }
            }
        }
        return iage;
        }
        return 0;
        }
0 0