タイムスタンプに基づいて指定された年齢を満たしているかどうかを判断します

フロントエンドによって渡されるタイムスタンプは、このデータに基づいて制限された年齢内であるかどうかを判断する必要があります(たとえば、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