Judging whether it meets the specified age based on the timestamp

The timestamp passed by the front end must be judged based on this data whether it is within the limited age (for example, it can only be 19 to 61 years old)

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

    }

Guess you like

Origin blog.csdn.net/PhilipJ0303/article/details/122413089