首页 > 其他分享 >身份证验证相关公共方法

身份证验证相关公共方法

时间:2023-02-02 15:56:21浏览次数:41  
标签:map return String 验证 idStr 身份证 公共 put Integer

 

身份证验证相关公共方法

 

package com.iktapp.api.utils;

import com.iktapp.api.constants.BusinessConstants;
import org.apache.logging.log4j.util.Strings;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *  身份证验证相关公共方法
 */
public class IDcardUtils
{

    /**
     *  校验码
     */
    public final static String[] CHECK_CODE = { "1", "0", "x", "9", "8", "7", "6", "5", "4", "3", "2" };

    /**
     * 加权银子
     */
    public final static String[] WEIGHTING_FACTOR = { "7", "9", "10", "5", "8", "4", "2", "1", "6", "3", "7", "9", "10", "5", "8", "4", "2" };

    /**
     * 日期正则
     */
    public final static String DATE_PATTERN = "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))" +
            "[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))" +
            "[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]" +
            "?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]" +
            "?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]" +
            "?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))" +
            "(\\s(((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$";

    /**
     * 数字正则
     */
    public final static String NUM_PATTERN = "[0-9]*";

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

    /**
     * 判断字符串是否为日期格式
     * @param strDate
     * @return
     */
    public static boolean isStrDate(String strDate)
    {
        Pattern pattern = Pattern.compile(DATE_PATTERN);
        Matcher m = pattern.matcher(strDate);
        if(m.matches())
        {
            return true;
        }
        return false;
    }

    /**
     * 判断字段是否为数字
     * @param str
     * @return
     */
    public static boolean isStrNum(String str) {
        Pattern pattern = Pattern.compile(NUM_PATTERN);
        Matcher isNum = pattern.matcher(str);
        if (isNum.matches())
        {
            return true;
        }
        return false;
    }



    /**
     * 检查身份证号码是否符合要求
     * @param idStr
     * @return
     */
    public static String checkIDcard(String idStr)
    {
        if(Strings.isBlank(idStr))
        {
            return "身份证号码为空";
        }
        if(idStr.length() != 18 && idStr.length() != 15)
        {
            return "身份证号码长度应该为15位或18位";
        }
        String iDCardNo = "";
        if (idStr.length() == 18)
        {
            iDCardNo = idStr.substring(0, 17);
        } else if (idStr.length() == 15)
        {
            iDCardNo = idStr.substring(0, 6) + "19" + idStr.substring(6, 15);
        }
        if (isStrNum(iDCardNo) == false) {
            return "身份证15位号码都应为数字;18位号码除最后一位外,都应为数字";
        }
        //判断出生年月
        // 年份
        String strYear = iDCardNo.substring(6, 10);
        // 月份
        String strMonth = iDCardNo.substring(10, 12);
        // 日
        String strDay = iDCardNo.substring(12, 14);
        if (isStrDate(strYear + "-" + strMonth + "-" + strDay) == false)
        {
            return "身份证生日无效";
        }
        GregorianCalendar gc = new GregorianCalendar();
        SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
        try {
            if ((gc.get(Calendar.YEAR) - Integer.parseInt(strYear)) > 150 || (gc.getTime().getTime() - s.parse(strYear + "-" + strMonth + "-" + strDay).getTime()) < 0) {
                return "身份证生日不在有效范围";
            }
            if (Integer.parseInt(strMonth) > 12 || Integer.parseInt(strMonth) == 0) {
                return "身份证月份无效";
            }
            if (Integer.parseInt(strDay) > 31 || Integer.parseInt(strDay) == 0) {
                return "身份证日期无效";
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return "身份证查询失败";
        }
        //判断地区码
        Map<String, String> province = provinceMap();
        if (province.get(iDCardNo.substring(0, 2)) == null) {
            return "身份证地区编码错误";
        }
        //判断最后一位
        int theLastOne = 0;
        for (int i = 0; i < 17; i++)
        {
            theLastOne += Integer.parseInt(String.valueOf(iDCardNo.charAt(i))) * Integer.parseInt(WEIGHTING_FACTOR[i]);
        }
        int modValue = theLastOne % 11;
        String strVerifyCode = CHECK_CODE[modValue];
        iDCardNo = iDCardNo + strVerifyCode;
        if(!iDCardNo.equalsIgnoreCase(idStr))
        {
            return "身份证最后一位无效,不是合法的身份证号码";
        }
        return BusinessConstants.SUCCESS;
    }

    /**
     * 根据身份证号计算年龄
     * @param pensonnelIdCard
     * @return
     */
    public static Integer getPersonAgeFromIdCard(String pensonnelIdCard) {

        //截取身份证中出行人出生日期中的年、月、日
        Integer personYear = Integer.parseInt(pensonnelIdCard.substring(6, 10));
        Integer personMonth = Integer.parseInt(pensonnelIdCard.substring(10, 12));
        Integer personDay = Integer.parseInt(pensonnelIdCard.substring(12, 14));

        Calendar cal = Calendar.getInstance();
        // 得到当前时间的年、月、日
        Integer yearNow = cal.get(Calendar.YEAR);
        Integer monthNow = cal.get(Calendar.MONTH) + 1;
        Integer dayNow = cal.get(Calendar.DATE);

        // 用当前年月日减去生日年月日
        Integer yearMinus = yearNow - personYear;
        Integer monthMinus = monthNow - personMonth;
        Integer dayMinus = dayNow - personDay;

        //先大致赋值
        Integer age = yearMinus;

        //出生年份为当前年份
        if (yearMinus == 0) {
            age = 0;
        }
        //出生年份大于当前年份
        else
        {
            //出生月份小于当前月份时,还没满周岁
            if (monthMinus < 0) {
                age = age - 1;
            }
            //当前月份为出生月份时,判断日期
            if (monthMinus == 0) {
                //出生日期小于当前月份时,没满周岁
                if (dayMinus < 0) {
                    age = age - 1;
                }
            }
        }
        return age;
    }

    /**
     * 根据省份证号获取生日
     * @param idStr
     * @return
     */
    public static String getBirthday(String idStr)
    {
        return idStr.substring(6, 10) + "-" +idStr.substring(10,12) + "-" +idStr.substring(12,14);
    }

    /**
     * 判断性别 true是女性 false是男性
     * @param idStr 身份证号
     * @return
     */
    public static Boolean isFemale(String idStr)
    {
       return Integer.parseInt(idStr.substring(16).substring(0, 1)) % 2 == 0 ;
    }

}

 

标签:map,return,String,验证,idStr,身份证,公共,put,Integer
From: https://www.cnblogs.com/xingmeng63/p/17086283.html

相关文章