计算出生年龄按规则输出

流过昼夜 提交于 2019-12-12 12:00:44

需求

具体就是根据出生时间判断是出生多久,并且按照出生的时间范围决定输出格式。

1星期内 : x天xx小时
 1星期以上-1个月以内 : xx天
 1个月以上-1周岁以内 : xx月xx天
 1周岁以上-3周岁以内 : xx岁xx月
 3周岁以上 : xx岁

思路

重点:
1使用Calendar类进行把出生时间和当前时间进行相减获取出生时间。
2通过出生时间计算出出生的总天数,并且使用总天数来进行范围的区分。
3考虑输入是yyyy-MM-dd HH:mm:ss,使用截取StringUtils工具类。

注意

Calendar类 月份指针是0代表1月,1代表二月,润平年的区分

上码

依赖

 <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
 public static void main(String[] args) throws ParseException {
        String s = parseAge("2015-11-11 08:06:00");
        System.out.println(s);
    }


    public static String parseAge(String birthdayInfo){
            String outPutStr="";
            //获取出生时间2019-5-1 8:06:00
            String birthdayInput = birthdayInfo;
            String beforeStrDate = StringUtils.substringBefore(birthdayInput, " ");
            String beforeStrDateYear = StringUtils.substringBefore(beforeStrDate, "-");
            String betweenStrDateMouth = StringUtils.substringBetween(beforeStrDate, "-");
            String afterLastStrDateDay = StringUtils.substringAfterLast(beforeStrDate, "-");
            String afterStrDate = StringUtils.substringAfter(birthdayInput, " ");
            String beforeStrDateHour = StringUtils.substringBefore(afterStrDate, ":");
            System.out.println("beforeStrDateHour:"+beforeStrDateHour);
            String betweenStrDateMinute = StringUtils.substringBetween(afterStrDate, ":");
            System.out.println("betweenStrDateMinute:"+betweenStrDateMinute);
            String afterLastStrDateSecond = StringUtils.substringAfterLast(afterStrDate, ":");
            System.out.println("afterLastStrDateSecond:"+afterLastStrDateSecond);
            Calendar birthday = new GregorianCalendar(Integer.parseInt(beforeStrDateYear), Integer.parseInt(betweenStrDateMouth), Integer.parseInt(afterLastStrDateDay),Integer.parseInt(beforeStrDateHour),Integer.parseInt(betweenStrDateMinute),Integer.parseInt(afterLastStrDateSecond));//2010年10月12日,month从0开始
            // 指定日期的格式
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //获取出生时间
            long yearSelectNum ;
            long hourNum =24;
            //使用Calendar计算年月日
            Calendar cal = Calendar.getInstance();
            int yearNow = cal.get(Calendar.YEAR);
            //计算年龄年月日
//        Date oldDateCal = birthday.getTime();
//        String old = simpleDateFormat.format(oldDateCal);
//        System.out.println("你的CAL出生日期" + old);
            int day = cal.get(Calendar.DAY_OF_MONTH) - birthday.get(Calendar.DAY_OF_MONTH);
            int month = cal.get(Calendar.MONTH) - birthday.get(Calendar.MONTH)+1;
            int year = cal.get(Calendar.YEAR) - birthday.get(Calendar.YEAR);
            //按照减法原理,先day相减,不够向month借;然后month相减,不够向year借;最后year相减。
            if(day<0){
                month -= 1;
                cal.add(Calendar.MONTH, -1);//得到上一个月,用来得到上个月的天数。
                day = day + cal.getActualMaximum(Calendar.DAY_OF_MONTH);
            }
            if(month<0){
                month = (month+12)%12;
                year--;
            }
            System.out.println("年龄:"+year+"年"+month+"月"+day+"天");
            //获取润平年
            boolean isYear = getIsYear(yearNow);
            //判断闰年或者平年
            if (isYear){
                yearSelectNum=365;
            }else{
                yearSelectNum=366;
            }
            // 直接创建现在的日期
            Date nowDate = new Date();
            // 将现在的日期转换成字符串
            String now = simpleDateFormat.format(nowDate);
            System.out.println("今天的日期是" + now);
            // 将出生日期和当前日期转换成毫秒值.getTime()
            Date oldDate = null;
            try {
                oldDate = simpleDateFormat.parse(birthdayInput);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            String oldInputDate = simpleDateFormat.format(oldDate);
            System.out.println("出生的日期是" + oldInputDate);
            long birthdayLong = oldDate.getTime();
            System.out.println("出生时间:"+birthdayLong);
            long nowday = nowDate.getTime();
            System.out.println("当前时间:"+nowday);
            long time = nowday - birthdayLong;
            System.out.println("差异时间:"+time);
            long daySum  = (time/1000/60 / 60 / 24);  // 除以计算得到出生天数
            System.out.println("出生总天数:"+daySum);
            long hour  = (time/ 1000 / 60 / 60 );  // 除以计算得到出生小时
            System.out.println("出生总小时:"+hour);
            //使用出生天数进行区分(利用总天数和小时数用作范围区分)
            if (daySum < 0) {
                System.out.println("还没出生呢");
            } else if (daySum>0 && daySum<=7){
                long dayStr = hour/hourNum;
                long hourStr = hour%hourNum;
                outPutStr=dayStr +"天" +hourStr+"小时";
            }else if  (daySum>7 && daySum<= getCurrentMonthDay()){
                long dayStr = hour/hourNum;
                outPutStr=dayStr +"天" ;
            }else if (daySum>=getCurrentMonthDay() &&daySum<yearSelectNum){
                outPutStr=month+"月" +day+"天";
            }else if (daySum>=yearSelectNum&&daySum<yearSelectNum*3){
                outPutStr=year +"岁" +month+"月";
            }else if (daySum>=yearSelectNum*3){
                outPutStr=year+"岁";
            }
        return outPutStr;
        }

    /**
     * 获取当月天数
     * @return
     */

    public static long getCurrentMonthDay() {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
         long maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    public static boolean getIsYear(int year){
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
            return true;//闰年
        else
            return false;//平年
    }

有更多想法,欢迎评论建议交流。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!