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

com.luues.util.card.IdCardUtil Maven / Gradle / Ivy

There is a newer version: 1.3.0.5.RELEASE
Show newest version
package com.luues.util.card;

import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.HashMap;

/**
 * Created by Administrator on 2016/2/25.
 */
public class IdCardUtil {
    /**
     * 中国公民身份证号码最小长度。
     */
    public final int CHINA_ID_MIN_LENGTH = 15;

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

    public static boolean verify(String idCards){
        IDCard idCard = new IDCard();
        return idCard.verify(idCards);
    }

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

    /**
     * 根据身份编号获取生日
     *
     * @param idCard 身份编号
     * @return 生日(yyyyMMdd)
     */
    public static String getBirthByIdCard(String idCard) {
        return idCard.substring(6, 14);
    }

    /**
     * 根据身份编号获取生日年
     *
     * @param idCard 身份编号
     * @return 生日(yyyy)
     */
    public static Short getYearByIdCard(String idCard) {
        return Short.valueOf(idCard.substring(6, 10));
    }

    /**
     * 根据身份编号获取生日月
     *
     * @param idCard 身份编号
     * @return 生日(MM)
     */
    public static Short getMonthByIdCard(String idCard) {
        return Short.valueOf(idCard.substring(10, 12));
    }

    /**
     * 根据身份编号获取生日天
     *
     * @param idCard 身份编号
     * @return 生日(dd)
     */
    public static Short getDateByIdCard(String idCard) {
        return Short.valueOf(idCard.substring(12, 14));
    }

    /**
     * 根据身份编号获取性别
     *
     * @param idCard 身份编号
     * @return 性别(1 - 男 , 2 - 女 , - 1 - 未知)
     */
    public static String getGenderByIdCard(String idCard) {
        String sGender = "-1";

        String sCardNum = idCard.substring(16, 17);
        if (Integer.parseInt(sCardNum) % 2 != 0) {
            sGender = "1";//男
        } else {
            sGender = "2";//女
        }
        return sGender;
    }

//修改年龄的方法
 /*   @RequestMapping(value = "shebaoshuju",method = RequestMethod.GET)
    public String shebaoshuju(){
        List list= wcbxxservice.all();
        int  cou=0;
        for(Wcbxx w:list){
            if(w.getIdcard()!=null && w.getIdcard().length()>17) {
                String sex = IdCard.getGenderByIdCard(w.getIdcard());
                int age = IdCard.getAgeByIdCard(w.getIdcard());
                w.setAge(Long.valueOf(age));
                w.setSex(sex);
                int a = wcbxxservice.updatess(w);
                if (a>0) {
                    System.out.println("姓名" + w.getRname() + "身份证" + w.getIdcard() + "年龄:" + age + "性别:" + sex);
                }
            }
            cou++;
            System.out.println(cou);
        }
        return "";
    }*/

    public static void main(String[] a) {
        String idcard = "513002199009031821 ";
        System.err.println(verify(idcard));
        String sex = getGenderByIdCard(idcard);
        System.out.println("性别:" + sex);
        int age = getAgeByIdCard(idcard);
        System.out.println("年龄:" + age);
        Short nian = getYearByIdCard(idcard);
        String yue = getMonthByIdCard(idcard).toString();
        String ri = getDateByIdCard(idcard).toString();
        yue = Integer.valueOf(yue) < 10 ? "0" + yue : yue;
        ri = Integer.valueOf(ri) < 10 ? "0" + ri : ri;
        System.out.println(nian + "年" + yue + "月" + ri + "日");
        String sr = nian + "-" + yue + "-" + ri;
        System.err.println(LocalDate.parse(sr, DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        sr = getBirthByIdCard(idcard);
        System.out.println("生日:" + sr);
    }
}

class IDCard {
    private static String _codeError;
    final int[] wi = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1};
    final int[] vi = {1, 0, 88, 9, 8, 7, 6, 5, 4, 3, 2};
    private int[] ai = new int[18];
    private static String[] _areaCode = {"11", "12", "13", "14", "15", "21", "22",
            "23", "31", "32", "33", "34", "35", "36", "37", "41", "42", "43", "44",
            "45", "46", "50", "51", "52", "53", "54", "61", "62", "63", "64", "65", "71", "81", "82", "91"};
    private static HashMap dateMap = new HashMap();
    private static HashMap areaCodeMap;

    static {
        dateMap.put("01", Integer.valueOf(31));
        dateMap.put("02", null);
        dateMap.put("03", Integer.valueOf(31));
        dateMap.put("04", Integer.valueOf(30));
        dateMap.put("05", Integer.valueOf(31));
        dateMap.put("06", Integer.valueOf(30));
        dateMap.put("07", Integer.valueOf(31));
        dateMap.put("08", Integer.valueOf(31));
        dateMap.put("09", Integer.valueOf(30));
        dateMap.put("10", Integer.valueOf(31));
        dateMap.put("11", Integer.valueOf(30));
        dateMap.put("12", Integer.valueOf(31));
        areaCodeMap = new HashMap();
        String[] arrayOfString;
        int j = (arrayOfString = _areaCode).length;
        for (int i = 0; i < j; i++) {
            String code = arrayOfString[i];
            areaCodeMap.put(code, null);
        }
    }

    public boolean verifyLength(String code) {
        int length = code.length();
        if ((length == 15) || (length == 18)) {
            return true;
        }
        _codeError = "错误:输入的身份证号不是15位和18位的";
        return false;
    }

    public boolean verifyAreaCode(String code) {
        String areaCode = code.substring(0, 2);
        if (areaCodeMap.containsKey(areaCode)) {
            return true;
        }
        _codeError = "错误:输入的身份证号的地区码(1-2位)[" + areaCode + "]不符合中国行政区划分代码规定(GB/T2260-1999)";
        return false;
    }

    public boolean verifyBirthdayCode(String code) {
        String month = code.substring(10, 12);
        boolean isEighteenCode = 18 == code.length();
        if (!dateMap.containsKey(month)) {
            _codeError = "错误:输入的身份证号" + (isEighteenCode ? "(11-12位)" : "(9-10位)") + "不存在[" + month + "]月份,不符合要求(GB/T7408)";
            return false;
        }
        String dayCode = code.substring(12, 14);
        Integer day = (Integer) dateMap.get(month);
        String yearCode = code.substring(6, 10);
        Integer year = Integer.valueOf(yearCode);
        if (day != null) {
            if ((Integer.valueOf(dayCode).intValue() > day.intValue()) || (Integer.valueOf(dayCode).intValue() < 1)) {
                _codeError = "错误:输入的身份证号" + (isEighteenCode ? "(13-14位)" : "(11-13位)") + "[" + dayCode + "]号不符合小月1-30天大月1-31天的规定(GB/T7408)";
                return false;
            }
        } else if (((year.intValue() % 4 == 0) && (year.intValue() % 100 != 0)) || (year.intValue() % 400 == 0)) {
            if ((Integer.valueOf(dayCode).intValue() > 29) || (Integer.valueOf(dayCode).intValue() < 1)) {
                _codeError = "错误:输入的身份证号" + (isEighteenCode ? "(13-14位)" : "(11-13位)") + "[" + dayCode + "]号在" + year + "闰年的情况下未符合1-29号的规定(GB/T7408)";
                return false;
            }
        } else if ((Integer.valueOf(dayCode).intValue() > 28) || (Integer.valueOf(dayCode).intValue() < 1)) {
            _codeError = "错误:输入的身份证号" + (isEighteenCode ? "(13-14位)" : "(11-13位)") + "[" + dayCode + "]号在" + year + "平年的情况下未符合1-28号的规定(GB/T7408)";
            return false;
        }
        return true;
    }

    public boolean containsAllNumber(String code) {
        String str = "";
        if (code.length() == 15) {
            str = code.substring(0, 15);
        } else if (code.length() == 18) {
            str = code.substring(0, 17);
        }
        char[] ch = str.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            if ((ch[i] < '0') || (ch[i] > '9')) {
                _codeError = "错误:输入的身份证号第" + (i + 1) + "位包含字母";
                return false;
            }
        }
        return true;
    }

    public String getCodeError() {
        return _codeError;
    }

    public boolean verify(String idcard) {
        _codeError = "";
        if (!verifyLength(idcard)) {
            return false;
        }
        if (!containsAllNumber(idcard)) {
            return false;
        }
        String eifhteencard = "";
        if (idcard.length() == 15) {
            eifhteencard = uptoeighteen(idcard);
        } else {
            eifhteencard = idcard;
        }
        if (!verifyAreaCode(eifhteencard)) {
            return false;
        }
        if (!verifyBirthdayCode(eifhteencard)) {
            return false;
        }
        if (!verifyMOD(eifhteencard)) {
            return false;
        }
        return true;
    }

    public boolean verifyMOD(String code) {
        String verify = code.substring(17, 18);
        if ("x".equals(verify)) {
            code = code.replaceAll("x", "X");
            verify = "X";
        }
        String verifyIndex = getVerify(code);
        if (verify.equals(verifyIndex)) {
            return true;
        }
        _codeError = "错误:输入的身份证号最末尾的数字验证码错误";
        return false;
    }

    public String getVerify(String eightcardid) {
        int remaining = 0;
        if (eightcardid.length() == 18) {
            eightcardid = eightcardid.substring(0, 17);
        }
        if (eightcardid.length() == 17) {
            int sum = 0;
            for (int i = 0; i < 17; i++) {
                String k = eightcardid.substring(i, i + 1);
                this.ai[i] = Integer.parseInt(k);
            }
            for (int i = 0; i < 17; i++) {
                sum += this.wi[i] * this.ai[i];
            }
            remaining = sum % 11;
        }
        return remaining == 2 ? "X" : String.valueOf(this.vi[remaining]);
    }

    public String uptoeighteen(String fifteencardid) {
        String eightcardid = fifteencardid.substring(0, 6);
        eightcardid = eightcardid + "19";
        eightcardid = eightcardid + fifteencardid.substring(6, 15);
        eightcardid = eightcardid + getVerify(eightcardid);
        return eightcardid;
    }

    public static void main(String[] args)
            throws IOException {
        String strIdCrad = "510283197910262848";

        boolean bIdCrad = new IDCard().verify(strIdCrad);
        System.out.println(bIdCrad + "+" + _codeError);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy