精准计算宝宝的年龄

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class TestAge {

    public static String getAge(Date nowDay, Date birthDay) throws Exception {
        Calendar cal = Calendar.getInstance();
        cal.setTime(nowDay);
        if (cal.before(birthDay)) {
            throw new IllegalArgumentException(
                    "出生时间大于当前时间!");
        }

        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH) + 1;//注意此处,如果不加1的话计算结果是错误的
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
        int monthMaxDayNow = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

        // 回到上个月
        // 修改点@0411
        cal.add(Calendar.MONTH,-1);
        int monthMaxDayPreFromNow = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

        cal.setTime(birthDay);

        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH)+1;
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
        int monthMaxDayBirth = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

        int age = yearNow - yearBirth;



        int months = 0;
        int days = 0;

        if(age == 0){// 当年出生
            if(monthNow == monthBirth){ // 当月出生
                 days = dayOfMonthNow - dayOfMonthBirth+1;
                return days+"天"; // todo 规则1:同年同月生显示多少天
            }else { // 非当月出生
                 months = monthNow - monthBirth;
                boolean bothLastDay = dayOfMonthNow == monthMaxDayNow && dayOfMonthBirth == monthMaxDayBirth;
                 days = 0;
                if (!bothLastDay && dayOfMonthNow < dayOfMonthBirth) {
                    months--;// 不足月,月份减去1
                    // 修改点@0411
                    int legacyDay = monthMaxDayPreFromNow-dayOfMonthBirth;
                    if(legacyDay < 0){ // "2022-1-31", "2022-3-28"
                        legacyDay = 0;
                    }
                    days = dayOfMonthNow+legacyDay;    // 修改点@0411
                    if(months == 0){
                        days++;
                    }
                    // todo 规则2:同年不同月天数不足情况
                } else {
                    //do nothing,足月
                    // todo 规则3:同年不同月天数足情况
                    days = dayOfMonthNow-dayOfMonthBirth;
                    if(days < 0){
                        days = 0;
                    }

                }
            }

            StringBuilder sb = new StringBuilder();
            if(months > 0){
                sb.append(months).append("个月");
            }
            if(days > 0){
                sb.append(days).append("天");
            }
            return sb.toString();
        }

        else if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {// 不可能多出月份
                //monthNow==monthBirth
                boolean bothLastDay = dayOfMonthNow == monthMaxDayNow && dayOfMonthBirth == monthMaxDayBirth;
                if (!bothLastDay && dayOfMonthNow < dayOfMonthBirth) {
                    age--;// 不足月,年龄减去1
                    months = 11;
                } else {
                    //do nothing,足月
                }
            } else {
                age--;
                months = 12-monthBirth+monthNow;
                boolean bothLastDay = dayOfMonthNow == monthMaxDayNow && dayOfMonthBirth == monthMaxDayBirth;
                if (!bothLastDay && dayOfMonthNow < dayOfMonthBirth) {
                    months--;// 不足月,月份减去1

                    // 修改点@0411
                    int legacyDay = monthMaxDayPreFromNow-dayOfMonthBirth;
                    if(legacyDay < 0){ // "2022-1-31", "2022-3-28"
                        legacyDay = 0;
                    }
                    days = dayOfMonthNow+legacyDay;      // 修改点@0411
                    if(months == 0){
                        days++;
                    }
                } else {
                    //do nothing,足月
                }
            }
        } else {
            //monthNow > monthBirth
            months = monthNow - monthBirth;
            boolean bothLastDay = dayOfMonthNow == monthMaxDayNow && dayOfMonthBirth == monthMaxDayBirth;
            // todo 1月31日到闰年2月29日或者平年2月28日为足月,计为一个月
            if (!bothLastDay && dayOfMonthNow < dayOfMonthBirth) { // 不足月
                months--;
            }
        }
        StringBuilder sb = new StringBuilder();
        if(age > 0){
            sb.append(age).append("岁");
        }
        if(months > 0){
            sb.append(months).append("个月");
        }
        if(age == 0){
            sb.append(days).append("天");
        }
        return sb.toString();
    }

    public static void main(String[] args) throws Exception {
//       cal("2020-12-3", "2020-12-3");
//       cal("2020-12-3", "2020-12-31");
//        cal("2020-1-31", "2020-02-29");
//        cal("2020-1-31", "2020-03-28");
//       cal("2020-1-31", "2020-03-01");
//       cal("2020-12-3", "2021-01-01");
//        cal("2022-2-28", "2022-3-31");
//        cal("2020-2-29", "2022-2-28");
//        cal("2020-2-29", "2024-2-28");
//        cal("2020-2-29", "2024-2-29");
//       cal("2020-2-29", "2024-3-28");
   //     cal("2020-2-29", "2024-3-31");
   //     cal("2022-2-28", "2022-3-11");
    //    cal("2022-2-28", "2022-4-11");
   //     cal("2022-2-28", "2022-4-28");
        cal("2022-11-30", "2023-3-28");
    }

    public static void cal(String birth, String nowStr ) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        Date birthDate = sdf.parse(birth);
        Date nowDate = sdf.parse(nowStr);
        String result  = getAge(nowDate, birthDate);
        System.out.println("出生"+birth+"当前"+nowStr+" 年龄:"+result);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值