java根据年月日精确计算带有年月日年龄

某天接到根据生日计算带年月日的岁数需求,立马开干,

代码如下:

public static String getAge(String birthDay) {
        if (birthDay == null) {
            return "未知";
        }

        String[] data = birthDay.split("-");

        if (data.length < 3) {
            return "";
        }
        Calendar birthday = new GregorianCalendar(Integer.valueOf(data[0]), Integer.valueOf(data[1]), Integer.valueOf(data[2]));
        Calendar now = Calendar.getInstance();
        int day = now.get(Calendar.DAY_OF_MONTH) - birthday.get(Calendar.DAY_OF_MONTH);
        //月份从0开始计算,所以需要+1
        int month = now.get(Calendar.MONTH)+1  - birthday.get(Calendar.MONTH);
        int year = now.get(Calendar.YEAR) - birthday.get(Calendar.YEAR);
        //按照减法原理,先day相减,不够向month借;然后month相减,不够向year借;最后year相减。
        if (day < 0) {
            month -= 1;
            now.add(Calendar.MONTH, -1);//得到上一个月,用来得到上个月的天数。
            day = day + now.getActualMaximum(Calendar.DAY_OF_MONTH);
        }
        if (month < 0) {
            month = (month + 12) % 12;
            year--;
        }
        StringBuffer tag = new StringBuffer();
        if (year > 0) {
            tag.append(year + "岁");
        }
        if (month > 0) {
            tag.append(month + "月");
        }
        if (day > 0) {
            tag.append(day + "天");
        }
        if (year == 0 && month == 0 && day == 0) {
            tag.append("今日出生");
        }
        return String.valueOf(tag);

    }

 

 

 

 

结果测试提了bug过来,说跟需求不一样,泪奔

一般不都是过生日的当天就+1岁了么,还要过今天才+1! 去找产品理论,没有说服产品,然后那改吧,还能咋样!

改过以后的代码为:

/**
     * 计算详细年龄
     * @param birthDay
     * @return 未满月, 几个月,几岁
     */
    public static String getChildAgeFromBirthTime(Date birthDay) {

        Calendar now = Calendar.getInstance();
        Calendar birthday = Calendar.getInstance();
        birthday.setTime(birthDay);
        int day = now.get(Calendar.DAY_OF_MONTH) - birthday.get(Calendar.DAY_OF_MONTH)-1;
        int month = now.get(Calendar.MONTH)  - birthday.get(Calendar.MONTH);
        int year = now.get(Calendar.YEAR) - birthday.get(Calendar.YEAR);
        if (day < 0) {
            month -= 1;
            now.add(Calendar.MONTH, -1);
            day = day + now.getActualMaximum(Calendar.DAY_OF_MONTH);
        }
        if (month < 0) {
            month = (month + 12) % 12;
            year--;
        }
        StringBuffer tag = new StringBuffer();
        if (year > 0) {
            tag.append(year + "岁");
        }
        if (month > 0) {
            tag.append(month + "月");
        }
        if (day > 0) {
            tag.append(day + "天");
        }
        if (year == 0 && month == 0 && day == 0) {
            tag.append("今日出生");
        }
        return String.valueOf(tag);


    }

 

最后完美解决问题!

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值