精准计算宝宝的年龄

Posted 小源子2016

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了精准计算宝宝的年龄相关的知识,希望对你有一定的参考价值。

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);
    

以上是关于精准计算宝宝的年龄的主要内容,如果未能解决你的问题,请参考以下文章

精准计算宝宝的年龄

php 实现根据身份证获取精准年龄

如何利用身份证号码计算出生年月,年龄及性别

30岁之后生的宝宝更聪明吗

假如我不知道今年是闰年还是平年计算明年的当前年月日(java)

运营商大数据精准获客