根据时间戳判断是否符合规定年龄

前端传递过来的时间戳,要根据此数据判断是否是在限定的年龄之内(比如只能是19到61岁)

 private void checkDob(long dob) throws ParseException {
        //规范化日期格式
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //将时间戳转换为实际的出生年月日
        String trueDate = simpleDateFormat.format(dob);

        //获取当前时间日期
        Date nowDate = new Date();
        String nowDate1 = simpleDateFormat.format(nowDate);

        Calendar front = Calendar.getInstance();
        Calendar later = Calendar.getInstance();

        front.setTime(simpleDateFormat.parse(trueDate));
        later.setTime(simpleDateFormat.parse(nowDate1));


        long dayGap = later.get(Calendar.DATE) - front.get(Calendar.DATE);
        //获取输入的生日和现在的时间相差多少个月
        long monthGap = later.get(Calendar.MONTH) - front.get(Calendar.MONTH);
        long yearToMonthGap = (later.get(Calendar.YEAR) - front.get(Calendar.YEAR)) * 12;
        dayGap = dayGap <= 0 ? 1 : 0;
        long finalResult = Math.abs(yearToMonthGap + monthGap) + dayGap;
        System.out.println(String.format("出生年月日和此刻时间相差 {%s} 个月..." , finalResult));

    }

猜你喜欢

转载自blog.csdn.net/PhilipJ0303/article/details/122413089