All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.dennisit.vplus.data.utils.IdcardUtils Maven / Gradle / Ivy

There is a newer version: 2.0.8
Show newest version
/*--------------------------------------------------------------------------
 *  Copyright (c) 2010-2020, Elon.su All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the elon developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: Elon.su, you can also mail [email protected]
 *--------------------------------------------------------------------------
 */
package com.github.dennisit.vplus.data.utils;

import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * Created by Elon.su on 2018/3/2.
 */
@Slf4j
public final class IdcardUtils {

    /**
     * 最低年限
     */
    public static final int MIN = 1930;

    /**
     * 中国公民身份证号码最小长度
     */
    public static final int CHINA_ID_MIN_LENGTH = 15;

    /**
     * 中国公民身份证号码最大长度
     */
    public static final int CHINA_ID_MAX_LENGTH = 18;

    /**
     * 每位加权因子
     */
    public static final int POWER[] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};

    /**
     * 大陆身份证省市辖区ID
     */
    public static Map CITY_CODES = Maps.newHashMap();

    /**
     * 台湾身份首字母对应数字
     */
    public static Map TW_CODE = Maps.newConcurrentMap();

    /**
     * 香港身份首字母对应数字
     */
    public static Map HK_CODE = Maps.newHashMap();

    static {
        CITY_CODES.put("11", "北京");
        CITY_CODES.put("12", "天津");
        CITY_CODES.put("13", "河北");
        CITY_CODES.put("14", "山西");
        CITY_CODES.put("15", "内蒙古");
        CITY_CODES.put("21", "辽宁");
        CITY_CODES.put("22", "吉林");
        CITY_CODES.put("23", "黑龙江");
        CITY_CODES.put("31", "上海");
        CITY_CODES.put("32", "江苏");
        CITY_CODES.put("33", "浙江");
        CITY_CODES.put("34", "安徽");
        CITY_CODES.put("35", "福建");
        CITY_CODES.put("36", "江西");
        CITY_CODES.put("37", "山东");
        CITY_CODES.put("41", "河南");
        CITY_CODES.put("42", "湖北");
        CITY_CODES.put("43", "湖南");
        CITY_CODES.put("44", "广东");
        CITY_CODES.put("45", "广西");
        CITY_CODES.put("46", "海南");
        CITY_CODES.put("50", "重庆");
        CITY_CODES.put("51", "四川");
        CITY_CODES.put("52", "贵州");
        CITY_CODES.put("53", "云南");
        CITY_CODES.put("54", "西藏");
        CITY_CODES.put("61", "陕西");
        CITY_CODES.put("62", "甘肃");
        CITY_CODES.put("63", "青海");
        CITY_CODES.put("64", "宁夏");
        CITY_CODES.put("65", "新疆");
        CITY_CODES.put("71", "台湾");
        CITY_CODES.put("81", "香港");
        CITY_CODES.put("82", "澳门");
        CITY_CODES.put("91", "国外");

        TW_CODE.put("A", 10);
        TW_CODE.put("B", 11);
        TW_CODE.put("C", 12);
        TW_CODE.put("D", 13);
        TW_CODE.put("E", 14);
        TW_CODE.put("F", 15);
        TW_CODE.put("G", 16);
        TW_CODE.put("H", 17);
        TW_CODE.put("J", 18);
        TW_CODE.put("K", 19);
        TW_CODE.put("L", 20);
        TW_CODE.put("M", 21);
        TW_CODE.put("N", 22);
        TW_CODE.put("P", 23);
        TW_CODE.put("Q", 24);
        TW_CODE.put("R", 25);
        TW_CODE.put("S", 26);
        TW_CODE.put("T", 27);
        TW_CODE.put("U", 28);
        TW_CODE.put("V", 29);
        TW_CODE.put("X", 30);
        TW_CODE.put("Y", 31);
        TW_CODE.put("W", 32);
        TW_CODE.put("Z", 33);
        TW_CODE.put("I", 34);
        TW_CODE.put("O", 35);

        HK_CODE.put("A", 1);
        HK_CODE.put("B", 2);
        HK_CODE.put("C", 3);
        HK_CODE.put("R", 18);
        HK_CODE.put("U", 21);
        HK_CODE.put("Z", 26);
        HK_CODE.put("X", 24);
        HK_CODE.put("W", 23);
        HK_CODE.put("O", 15);
        HK_CODE.put("N", 14);
    }


    public static boolean validateCard(String idcard) {
        String card = idcard.trim();
        if (validateIdcard18(card)) {
            return true;
        }
        if (validateIdcard15(card)) {
            return true;
        }
        String[] value = validateIdcard10(card);
        if (value != null) {
            if (value[2].equals("true")) {
                return true;
            }
        }
        return false;
    }


    public static int getPowerSum(int[] iArr) {
        int sum = 0;
        if (POWER.length == iArr.length) {
            for (int i = 0; i < iArr.length; i++) {
                for (int j = 0; j < POWER.length; j++) {
                    if (i == j) {
                        sum = sum + iArr[i] * POWER[j];
                    }
                }
            }
        }
        return sum;
    }

    /**
     * 将power和值与11取模获得余数进行校验码判断
     *
     * @param sum 数字
     * @return 校验位
     */
    private static String getCheckCode18(int sum) {
        String sCode = "";
        switch (sum % 11) {
            case 10:
                sCode = "2";
                break;
            case 9:
                sCode = "3";
                break;
            case 8:
                sCode = "4";
                break;
            case 7:
                sCode = "5";
                break;
            case 6:
                sCode = "6";
                break;
            case 5:
                sCode = "7";
                break;
            case 4:
                sCode = "8";
                break;
            case 3:
                sCode = "9";
                break;
            case 2:
                sCode = "x";
                break;
            case 1:
                sCode = "0";
                break;
            case 0:
                sCode = "1";
                break;
        }
        return sCode;
    }

    /**
     * 根据身份编号获取年龄
     *
     * @param idcard 身份编号
     * @return 年龄
     */
    public static int getAgeByIdCard(String idcard) {
        int iAge = 0;
        if (idcard.length() == CHINA_ID_MIN_LENGTH) {
            idcard = convertIdcard15To18(idcard);
        }
        String year = idcard.substring(6, 10);
        Calendar cal = Calendar.getInstance();
        int iCurrYear = cal.get(Calendar.YEAR);
        iAge = iCurrYear - Integer.valueOf(year);
        return iAge;
    }

    /**
     * 根据身份编号获取生日
     *
     * @param idcard 身份编号
     * @return 生日(yyyyMMdd)
     */
    public static String getBirthByIdCard(String idcard) {
        Integer len = idcard.length();
        if (len < CHINA_ID_MIN_LENGTH) {
            return null;
        } else if (len == CHINA_ID_MIN_LENGTH) {
            idcard = convertIdcard15To18(idcard);
        }
        return idcard.substring(6, 14);
    }

    /**
     * 根据身份编号获取生日年
     *
     * @param idCard 身份编号
     * @return 生日(yyyy)
     */
    public static Short getYearByIdCard(String idCard) {
        Integer len = idCard.length();
        if (len < CHINA_ID_MIN_LENGTH) {
            return null;
        } else if (len == CHINA_ID_MIN_LENGTH) {
            idCard = convertIdcard15To18(idCard);
        }
        return Short.valueOf(idCard.substring(6, 10));
    }

    /**
     * 根据身份编号获取生日月
     *
     * @param idcard 身份编号
     * @return 生日(MM)
     */
    public static Short getMonthByIdCard(String idcard) {
        Integer len = idcard.length();
        if (len < CHINA_ID_MIN_LENGTH) {
            return null;
        } else if (len == CHINA_ID_MIN_LENGTH) {
            idcard = convertIdcard15To18(idcard);
        }
        return Short.valueOf(idcard.substring(10, 12));
    }

    /**
     * 根据身份编号获取生日天
     *
     * @param idcard 身份编号
     * @return 生日(dd)
     */
    public static Short getDateByidcard(String idcard) {
        Integer len = idcard.length();
        if (len < CHINA_ID_MIN_LENGTH) {
            return null;
        } else if (len == CHINA_ID_MIN_LENGTH) {
            idcard = convertIdcard15To18(idcard);
        }
        return Short.valueOf(idcard.substring(12, 14));
    }

    /**
     * 根据身份编号获取性别
     *
     * @param idcard 身份编号
     * @return 性别(M - 男 , F - 女 , N - 未知)
     */
    public static String getGenderByIdCard(String idcard) {
        String sGender = "N";
        if (idcard.length() == CHINA_ID_MIN_LENGTH) {
            idcard = convertIdcard15To18(idcard);
        }
        String sCardNum = idcard.substring(16, 17);
        if (Integer.parseInt(sCardNum) % 2 != 0) {
            sGender = "M";
        } else {
            sGender = "F";
        }
        return sGender;
    }

    /**
     * 根据身份编号获取户籍省份
     *
     * @param idCard 身份编码
     * @return 省级编码。
     */
    public static String getProvinceByIdCard(String idCard) {
        int len = idCard.length();
        String sProvince = null;
        String sProvinNum = "";
        if (len == CHINA_ID_MIN_LENGTH || len == CHINA_ID_MAX_LENGTH) {
            sProvinNum = idCard.substring(0, 2);
        }
        sProvince = CITY_CODES.get(sProvinNum);
        return sProvince;
    }


    private static String convertIdcard15To18(String idcard) {
        String idCard18 = "";
        if (idcard.length() != CHINA_ID_MIN_LENGTH) {
            return null;
        }
        if (idcard.matches("^[0-9]*$")) {
            // 获取出生年月日
            String birthday = idcard.substring(6, 12);
            Date birthDate = null;
            try {
                birthDate = new SimpleDateFormat("yyMMdd").parse(birthday);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            Calendar cal = Calendar.getInstance();
            if (birthDate != null)
                cal.setTime(birthDate);
            // 获取出生年(完全表现形式,如:2010)
            String sYear = String.valueOf(cal.get(Calendar.YEAR));
            idCard18 = idcard.substring(0, 6) + sYear + idcard.substring(8);
            // 转换字符数组
            char[] cArr = idCard18.toCharArray();
            if (cArr != null) {
                int[] iCard = NumberUtils.charToInt(cArr);
                int iSum17 = getPowerSum(iCard);
                // 获取校验位
                String sVal = getCheckCode18(iSum17);
                if (sVal.length() > 0) {
                    idCard18 += sVal;
                } else {
                    return null;
                }
            }
        } else {
            return null;
        }
        return idCard18;
    }


    /**
     * 验证18位身份编码是否合法
     *
     * @param idcard 身份编码
     * @return 是否合法
     */
    private static boolean validateIdcard18(String idcard) {
        boolean bTrue = false;
        if (idcard.length() == CHINA_ID_MAX_LENGTH) {
            // 前17位
            String code17 = idcard.substring(0, 17);
            // 第18位
            String code18 = idcard.substring(17, CHINA_ID_MAX_LENGTH);
            if (code17.matches("^[0-9]*$")) {
                char[] cArr = code17.toCharArray();
                if (cArr != null) {
                    int[] iCard = NumberUtils.charToInt(cArr);
                    int iSum17 = getPowerSum(iCard);
                    // 获取校验位
                    String val = getCheckCode18(iSum17);
                    if (val.length() > 0) {
                        if (val.equalsIgnoreCase(code18)) {
                            bTrue = true;
                        }
                    }
                }
            }
        }
        return bTrue;
    }


    /**
     * 验证15位身份编码是否合法
     *
     * @param idcard 身份编码
     * @return 是否合法
     */
    private static boolean validateIdcard15(String idcard) {
        if (idcard.length() != CHINA_ID_MIN_LENGTH) {
            return false;
        }
        if (idcard.matches("^[0-9]*$")) {
            String proCode = idcard.substring(0, 2);
            if (CITY_CODES.get(proCode) == null) {
                return false;
            }
            String birthCode = idcard.substring(6, 12);
            Date birthDate = null;
            try {
                birthDate = new SimpleDateFormat("yy").parse(birthCode.substring(0, 2));
            } catch (ParseException e) {
                log.info(e.getLocalizedMessage(), e);
            }
            Calendar cal = Calendar.getInstance();
            if (birthDate != null)
                cal.setTime(birthDate);
            if (!validateDate(cal.get(Calendar.YEAR), Integer.valueOf(birthCode.substring(2, 4)), Integer.valueOf(birthCode.substring(4, 6)))) {
                return false;
            }
        } else {
            return false;
        }
        return true;
    }

    /**
     * 验证10位身份编码是否合法
     *
     * @param idcard 身份编码
     * @return 身份证信息数组 [0] - 台湾、澳门、香港 [1] - 性别(男M,女F,未知N) [2] - 是否合法(合法true,不合法false)
     * s
     */
    private static String[] validateIdcard10(String idcard) {
        String[] info = new String[3];
        String card = idcard.replaceAll("[\\(|\\)]", "");
        if (card.length() != 8 && card.length() != 9 && idcard.length() != 10) {
            return null;
        }
        if (idcard.matches("^[a-zA-Z][0-9]{9}$")) { // 台湾
            info[0] = "台湾";
            String char2 = idcard.substring(1, 2);
            if (char2.equals("1")) {
                info[1] = "M";
            } else if (char2.equals("2")) {
                info[1] = "F";
            } else {
                info[1] = "N";
                info[2] = "false";
                return info;
            }
            info[2] = validateTWCard(idcard) ? "true" : "false";
        } else if (idcard.matches("^[1|5|7][0-9]{6}\\(?[0-9A-Z]\\)?$")) { // 澳门
            info[0] = "澳门";
            info[1] = "N";
        } else if (idcard.matches("^[A-Z]{1,2}[0-9]{6}\\(?[0-9A]\\)?$")) { // 香港
            info[0] = "香港";
            info[1] = "N";
            info[2] = validateHKCard(idcard) ? "true" : "false";
        } else {
            return null;
        }
        return info;
    }

    /**
     * 验证台湾身份证号码
     *
     * @param idcard 身份证号码
     * @return 验证码是否符合
     */
    public static boolean validateTWCard(String idcard) {
        String start = idcard.substring(0, 1);
        String mid = idcard.substring(1, 9);
        String end = idcard.substring(9, 10);
        Integer iStart = TW_CODE.get(start);
        Integer sum = iStart / 10 + (iStart % 10) * 9;
        char[] chars = mid.toCharArray();
        Integer flag = 8;
        for (char c : chars) {
            sum = sum + Integer.valueOf(c + "") * flag;
            flag--;
        }
        return (sum % 10 == 0 ? 0 : (10 - sum % 10)) == Integer.valueOf(end) ? true : false;
    }


    /**
     * 验证香港身份证号码(存在Bug,部份特殊身份证无法检查)
     * 身份证前2位为英文字符,如果只出现一个英文字符则表示第一位是空格,对应数字58 前2位英文字符A-Z分别对应数字10-35
     * 最后一位校验码为0-9的数字加上字符"A","A"代表10
     * 将身份证号码全部转换为数字,分别对应乘9-1相加的总和,整除11则证件号码有效
     *
     * @param idCard 身份证号码
     * @return 验证码是否符合
     */
    public static boolean validateHKCard(String idCard) {
        String card = idCard.replaceAll("[\\(|\\)]", "");
        Integer sum = 0;
        if (card.length() == 9) {
            sum = (Integer.valueOf(card.substring(0, 1).toUpperCase().toCharArray()[0]) - 55) * 9
                    +
                    (Integer.valueOf(card.substring(1, 2).toUpperCase().toCharArray()[0]) - 55) * 8;
            card = card.substring(1, 9);
        } else {
            sum = 522 + (Integer.valueOf(card.substring(0, 1).toUpperCase().toCharArray()[0]) - 55) * 8;
        }
        String mid = card.substring(1, 7);
        String end = card.substring(7, 8);
        char[] chars = mid.toCharArray();
        Integer flag = 7;
        for (char c : chars) {
            sum = sum + Integer.valueOf(c + "") * flag;
            flag--;
        }
        if (end.toUpperCase().equals("A")) {
            sum = sum + 10;
        } else {
            sum = sum + Integer.valueOf(end);
        }
        return (sum % 11 == 0) ? true : false;
    }

    private static boolean validateDate(int iYear, int iMonth, int iDate) {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int datePerMonth;
        if (iYear < MIN || iYear >= year) {
            return false;
        }
        if (iMonth < 1 || iMonth > 12) {
            return false;
        }
        switch (iMonth) {
            case 4:
            case 6:
            case 9:
            case 11:
                datePerMonth = 30;
                break;
            case 2:
                boolean dm = ((iYear % 4 == 0 && iYear % 100 != 0) || (iYear % 400 == 0))
                        && (iYear > MIN && iYear < year);
                datePerMonth = dm ? 29 : 28;
                break;
            default:
                datePerMonth = 31;
        }
        return (iDate >= 1) && (iDate <= datePerMonth);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy