检验身份证号码

/**
 *
 */


import java.text.SimpleDateFormat;

/**
 * @author liu
 * @version 2017年4月5日 下午4:11:07
 * @purpose
 */
public class Snippet {
    private static boolean checkDate(String year, String month, String day) {
            SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyyMMdd");
            try {
                String s3 = year + month + day;
                simpledateformat.setLenient(false);
                simpledateformat.parse(s3);
            } catch (java.text.ParseException parseexception) {
                return false;
            }
            return true;
        }
    
        /**
         * 校验身份证
         *
         * @param certiCode
         *            待校验身份证
         * @return 0--校验成功; 1--位数不对; 2--生日格式不对 ; 3--校验位不对 ; 4--其他异常;5--字符异常;
         * @param certiCode
         * @return
         */
    public static int checkCertiCode(String certiCode) {
            try {
                if (certiCode == null || certiCode.length() != 15
                        && certiCode.length() != 18)
                    return 1;
                String s1;
                String s2;
                String s3;
                if (certiCode.length() == 15) {
                    if (!checkFigure(certiCode)) {
                        return 5;
                    }
                    s1 = "19" + certiCode.substring(6, 10);
                    s2 = certiCode.substring(8, 10);
                    s3 = certiCode.substring(10, 12);
                    if (!checkDate(s1, s2, s3))
                        return 2;
                }
                if (certiCode.length() == 18) {
                    if (!checkFigure(certiCode.substring(0, 17))) {
                        return 5;
                    }
                    s1 = certiCode.substring(6, 10);
                    s2 = certiCode.substring(10, 12);
                    s3 = certiCode.substring(12, 14);
                    if (!checkDate(s1, s2, s3))
                        return 2;
                    if (!checkIDParityBit(certiCode))
                        return 3;
                }
            } catch (Exception exception) {
                return 4;
            }
            return 0;
        }
        
        /**
         * 检查字符串是否全为数字
         *
         * @param certiCode
         * @return
         */
        private static boolean checkFigure(String certiCode) {
            try {
                Long.parseLong(certiCode);
            } catch (NumberFormatException e) {
                return false;
            }
            return true;
        }
        
        private static boolean checkIDParityBit(String certiCode) {
            boolean flag = false;
            if (certiCode == null || "".equals(certiCode))
                return false;
            int ai[] = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 };
            if (certiCode.length() == 18) {
                int i = 0;
                for (int k = 0; k < 18; k++) {
                    char c = certiCode.charAt(k);
                    int j;
                    if (c == 'X'||c == 'x')
                        j = 10;
                    else if (c <= '9' || c >= '0')
                        j = c - 48;
                    else
                        return flag;
                    i += j * ai[k];
                }
                if (i % 11 == 1)
                    flag = true;
            }
            return flag;
        }
        
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值