jiangping
2024-05-17 fb7eaacbfbbcc9ac376735586c0ee0aae32f5384
server/service/src/main/java/com/doumee/core/utils/Constants.java
@@ -156,18 +156,22 @@
    }
    public static long getAgeByIdCard(String idCard){
        int birthYear = Integer.parseInt(idCard.substring(6, 10));
        int birthMonth = Integer.parseInt(idCard.substring(10, 12));
        int birthDay = Integer.parseInt(idCard.substring(12, 14));
        try{
            LocalDate birthDate = LocalDate.of(birthYear, birthMonth, birthDay);
            LocalDate currentDate = LocalDate.now();
            long age = ChronoUnit.YEARS.between(birthDate, currentDate);
            return age;
        try {
            int birthYear = Integer.parseInt(idCard.substring(6, 10));
            int birthMonth = Integer.parseInt(idCard.substring(10, 12));
            int birthDay = Integer.parseInt(idCard.substring(12, 14));
            try{
                LocalDate birthDate = LocalDate.of(birthYear, birthMonth, birthDay);
                LocalDate currentDate = LocalDate.now();
                long age = ChronoUnit.YEARS.between(birthDate, currentDate);
                return age;
            }catch (Exception e){
                throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"身份证号码错误:"+idCard);
            }
        }catch (Exception e){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"身份证号码错误:"+idCard);
        }
        }
        return 0;
    }