JAVA:18位身份证号码验证工具类(识别性别和生日、计算年龄)

提供了一个JAVA工具类,用于校验18位身份证号码的正确性,同时能解析身份证号码获取性别和出生日期,并计算个人年龄。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.ljl.util;

import java.util.Calendar;
import java.util.stream.IntStream;

/**
 * 身份证号码验证
 * 1、号码的结构
 * 公民身份号码是特征组合码,由十七位数字本体码和一位校验码组成。从左至右依次为:六位数字地址码,
 * 八位数字出生日期码,三位数字顺序码和一位数字校验码。
 * 2、地址码(前六位数)
 * 表示编码对象常住户口所在县(市、旗、区)的行政区划代码,按GB/T2260的规定执行。
 * 3、出生日期码(第七位至十四位)
 * 表示编码对象出生的年、月、日,按GB/T7408的规定执行,年、月、日代码之间不用分隔符。
 * 4、顺序码(第十五位至十七位)
 * 表示在同一地址码所标识的区域范围内,对同年、同月、同日出生的人编定的顺序号,
 * 顺序码的奇数分配给男性,偶数分配给女性。
 * 5、校验码(第十八位数)
 * (1)十七位数字本体码加权求和公式 S = Sum(Ai Wi), i = 0, , 16 ,先对前17位数字的权求和 ;
 * Ai:表示第i位置上的身份证号码数字值; Wi:表示第i位置上的加权因子 Wi: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2
 * (2)计算模 Y = mod(S, 11)
 * (3)通过模( 0 1 2 3 4 5 6 7 8 9 10)得到对应的校验码 Y:1 0 X 9 8 7 6 5 4 3 2
 */
public class IdentityUtils {

    // 身份证校验码
    private static final int[] COEFFICIENT_ARRAY = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};

    // 身份证号的尾数规则
    private static final String[] IDENTITY_MANTISSA = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};

    private static final String IDENTITY_PATTERN = "^[0-9]{17}[0-9Xx]$";

    public static boolean isLegalPattern(String identity) {
        if (identity == null) {
            return false;
        }

        if (identity.length() != 18) {
            return false;
        }

        if (!identity.matches(IDENTITY_PATTERN)) {
            return false;
        }

        char[] chars = identity.toCharArray();
        long sum = IntStream.range(0, 17).map(index -> {
            char ch = chars[index];
            int digit = Character.digit(ch, 10);
            int coefficient = COEFFICIENT_ARRAY[index];
            return digit * coefficient;
        }).summaryStatistics().getSum();

        // 计算出的尾数索引
        int mantissaIndex = (int) (sum % 11);
        String mantissa = IDENTITY_MANTISSA[mantissaIndex];

        String lastChar = identity.substring(17);
        if (lastChar.equalsIgnoreCase(mantissa)) {
            return true;
        } else {
            return false;
        }
    }

    //计算年龄
    public static int getAgefromBirthTime(String birthTimeString){

        // 先截取到字符串中的年、月、日
        String strs[] = birthTimeString.trim().split("-");
        int selectYear = Integer.parseInt(strs[0]);
        int selectMonth = Integer.parseInt(strs[1]);
        int selectDay = Integer.parseInt(strs[2]);
        // 得到当前时间的年、月、日
        Calendar cal = Calendar.getInstance();
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH) + 1;
        int dayNow = cal.get(Calendar.DATE);

        // 用当前年月日减去生日年月日
        int yearMinus = yearNow - selectYear -1;
        int monthMinus = monthNow - selectMonth;
        int dayMinus = dayNow - selectDay;

        int age = yearMinus;
        if (yearMinus < 0) {// 选了未来的年份
            age = 0;
        } else if (yearMinus == 0) {// 同年的,要么为1,要么为0
            if (monthMinus < 0) {// 选了未来的月份
                age = 0;
            } else if (monthMinus == 0) {// 同月份的
                if (dayMinus < 0) {// 选了未来的日期
                    age = 0;
                } else if (dayMinus >= 0) {
                    age = 1;
                }
            } else if (monthMinus > 0) {
                age = 1;
            }
        } else if (yearMinus > 0) {
            if (monthMinus < 0) {// 当前月>生日月
            } else if (monthMinus == 0) {// 同月份的,再根据日期计算年龄
                if (dayMinus < 0) {
                } else if (dayMinus >= 0) {
                    age = age + 1;
                }
            } else if (monthMinus > 0) {
                age = age + 1;
            }
        }
        return age;

    }
}

很好的校验身份证工具类,忘记在哪个网站找的了,现保存一下以后可以再用到。

if (IdentityUtils.isLegalPattern(data.getArchivesIdcard())){
   //识别性别和生日并计算年龄
   String sex;
	if (Integer.parseInt(CardCode.substring(16).substring(0, 1)) % 2 == 0) {// 判断性别
		sex = "女";
	} else {
		sex = "男";
	}

    String birthday = "";
    birthday = data.getArchivesIdcard().substring(6,10)+"年"+data.getArchivesIdcard().substring(10,12)
            +"月"+data.getArchivesIdcard().substring(12,14)+"日";
    data.setArchivesBirthday(birthday);
    String birthDate = "";
    birthDate = data.getArchivesIdcard().substring(6,10)+"-"+data.getArchivesIdcard().substring(10,12)+"-"+data.getArchivesIdcard().substring(12,14);
    data.setArchivesAges(IdentityUtils.getAgefromBirthTime(birthDate));
}else{
    result.put("data", "");
    result.put("errcode", 1001);
    result.put("errmsg", "请检查身份证是否正确");
    return result;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值