Java实现年龄计算

一、根据生日精确计算年龄

    public static int age(Date birthDate) {
        // 当前日历
        Calendar nowCalendar = Calendar.getInstance();
        // 生日大于当前日期
        if (nowCalendar.before(birthDate)) {
            throw new IllegalArgumentException("The birth date is before current time, it's unbelievable");
        }
        // 当前年月日
        int yearNow = nowCalendar.get(Calendar.YEAR);
        int monthNow = nowCalendar.get(Calendar.MONTH);
        int dayNow = nowCalendar.get(Calendar.DAY_OF_MONTH);
        // 出生日期年月日
        Calendar birthCalendar = Calendar.getInstance();
        birthCalendar.setTime(birthDate);
        int yearBirth = birthCalendar.get(Calendar.YEAR);
        int monthBirth = birthCalendar.get(Calendar.MONTH);
        int dayBirth = birthCalendar.get(Calendar.DAY_OF_MONTH);
        // 粗计算年龄
        int age = yearNow - yearBirth;
        // 当前月份小于出生月份年龄减一
        if (monthNow < monthBirth) { age--; }
        // 当前月份等于出生月份,日小于生日年龄减一
        else if (monthNow == monthBirth && dayNow < dayBirth) { age--; }
        // 返回年龄值
        return age;
    }

二、年龄不足1周岁的月龄,以分数形式表示

   /**
     * 年龄不足1周岁的月龄,以分数形式表示,
     * 分数的整数部分为实足月龄,分数部分分母为30,
     * 分子为不足1个月的天数。
     * 如 3个月15天表达为:3 15/30
     *
     * @param birthDate 出生日期
     * @return 月龄值
     */
    public static String mothAge(Date birthDate) {
        Calendar nowCalendar = Calendar.getInstance();
        int monthNow = nowCalendar.get(Calendar.MONTH);
        int dayNow = nowCalendar.get(Calendar.DAY_OF_MONTH);

        Calendar birthCalendar = Calendar.getInstance();
        birthCalendar.setTime(birthDate);
        int monthBirth = birthCalendar.get(Calendar.MONTH);
        int dayBirth = birthCalendar.get(Calendar.DAY_OF_MONTH);
        int month = monthNow - monthBirth;
        month = month > 0 ? month : (12 + month);

        if (dayNow >= dayBirth) {
            return month + StringPools.SPACE + (dayNow - dayBirth) + "/30";
        }
        return month - 1 + StringPools.SPACE + (30 - dayBirth + dayNow) + "/30";
    }

三、根据出生日期-当前日期计算

   /**
     * 根据出生日期-当前日期计算,成人写整数即可;
     * 儿科的,一个月内写天数,
     * 半岁以内写几个月几天,
     * 半岁以上写几个月,
     * 一岁以上写几岁几个月,
     * 12岁以上用成人的写法
     *
     * @param birthDate 生日
     * @return 年龄
     * @author xiao.xl 2022/2/16 10:35
     */
    public static String getPatientAge(Date birthDate) {
        // 当前日历
        Calendar nowCalendar = Calendar.getInstance();
        // 生日大于当前日期
        if (nowCalendar.before(birthDate)) {
            throw new IllegalArgumentException("The birth date is before current time, it's unbelievable");
        }
        // 当前年月日
        int yearNow = nowCalendar.get(Calendar.YEAR);
        int monthNow = nowCalendar.get(Calendar.MONTH);
        int dayNow = nowCalendar.get(Calendar.DAY_OF_MONTH);
        // 出生日期年月日
        Calendar birthCalendar = Calendar.getInstance();
        birthCalendar.setTime(birthDate);
        int yearBirth = birthCalendar.get(Calendar.YEAR);
        int monthBirth = birthCalendar.get(Calendar.MONTH);
        int dayBirth = birthCalendar.get(Calendar.DAY_OF_MONTH);
        // 粗计算年龄
        int age = yearNow - yearBirth;
        // 当前月份小于出生月份年龄减一
        if (monthNow < monthBirth) { age--; }
        // 当前月份等于出生月份,日小于生日年龄减一
        else if (monthNow == monthBirth && dayNow < dayBirth) { age--; }
        // 粗计月
        int month;
        if (monthNow < monthBirth) {
            month = 12 + monthNow - monthBirth;
        } else {
            month = monthNow - monthBirth;
        }
        int day;
        if (dayNow < dayBirth) {
            day = 30 + dayNow - dayBirth;
            month = month - 1;
        } else {
            day = dayNow - dayBirth;
        }
        if (age >= 12) {
           return age + "岁";
        } else if (age >= 1) {
            return age + "岁 " + month + "月";
        } else if (month >= 6) {
            return month + "月";
        } else if (month >= 1) {
            return month + "月 " + day + "天";
        }
        return day + "天";
    }

你可能感兴趣的:(工具类,java,开发语言)