根据身份证号获取性别

1.

public class Test {
    /**
     * 修补15位居民身份证号码为18位,不校验身份证有效性
     *
     * @param personIDCode
     *            十五位身份证号码
     * @return 十八位身份证号码
     * @throws 身份证号参数不是15位
     */
    public static String fixPersonIDCodeWithoutCheck(String personIDCode) throws Exception {
        if (personIDCode == null || personIDCode.trim().length() != 15)
            throw new Exception("输入的身份证号不足15位,请检查");

        String id17 = personIDCode.substring(0, 6) + "19" + personIDCode.substring(6, 15); // 15位身份证补'19'

        char[] code = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' }; // 11个校验码字符
        int[] factor = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 }; // 18个加权因子
        int[] idcd = new int[18];
        int sum; // 根据公式 ∑(ai×Wi) 计算
        int remainder; // 第18位校验码
        for (int i = 0; i < 17; i++) {
            idcd[i] = Integer.parseInt(id17.substring(i, i + 1));
        }
        sum = 0;
        for (int i = 0; i < 17; i++) {
            sum = sum + idcd[i] * factor[i];
        }
        remainder = sum % 11;
        String lastCheckBit = String.valueOf(code[remainder]);
        return id17 + lastCheckBit;
    }
    /**
     * 修补15位居民身份证号码为18位,并校验15位身份证有效性
     *
     * @param personIDCode
     *            十五位身份证号码
     * @return String 十八位身份证号码
     * @throws 无效的身份证号
     */
    public static String fixPersonIDCodeWithCheck(String personIDCode) throws Exception {
        if (personIDCode == null || personIDCode.trim().length() != 15)
            throw new Exception("输入的身份证号不足15位,请检查");

        if (!isIdentity(personIDCode))
            throw new Exception("输入的身份证号无效,请检查");

        return fixPersonIDCodeWithoutCheck(personIDCode);
    }
    /**
     * 判断是否是有效的18位或15位居民身份证号码
     *
     * @param identity
     *            18位或15位居民身份证号码
     * @return 是否为有效的身份证号码
     */
    public static boolean isIdentity(String identity) {
        if (identity == null)
            return false;
        if (identity.length() == 18 || identity.length() == 15) {
            String id15 = null;
            if (identity.length() == 18)
                id15 = identity.substring(0, 6) + identity.substring(8, 17);
            else
                id15 = identity;
            try {
                Long.parseLong(id15); // 校验是否为数字字符串

                String birthday = "19" + id15.substring(6, 12);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                sdf.parse(birthday); // 校验出生日期
                if (identity.length() == 18 && !fixPersonIDCodeWithoutCheck(id15).equals(identity))
                    return false; // 校验18位身份证
            } catch (Exception e) {
                return false;
            }
            return true;
        } else
            return false;
    }

    /**
     * 将传入的身份证号码进行校验,并返回一个对应的18位身份证
     *
     * @param personIDCode
     *            身份证号码
     * @return String 十八位身份证号码
     * @throws 无效的身份证号
     */
    public static String getFixedPersonIDCode(String personIDCode) throws Exception {
        if (personIDCode == null)
            throw new Exception("输入的身份证号无效,请检查");

        if (personIDCode.length() == 18) {
            if (isIdentity(personIDCode))
                return personIDCode;
            else
                throw new Exception("输入的身份证号无效,请检查");
        } else if (personIDCode.length() == 15)
            return fixPersonIDCodeWithCheck(personIDCode);
        else
            throw new Exception("输入的身份证号无效,请检查");
    }
    /**
     * 从身份证号获取性别  1:男  2:女
     *
     * @param identity
     *            身份证号
     * @return 性别代码
     * @throws Exception
     *             无效的身份证号码
     */
    public static String getGenderFromPersonIDCode(String identity) throws Exception {
        String id = getFixedPersonIDCode(identity);
        char sex = id.charAt(16);
        return sex % 2 == 0 ? "2" : "1";
    }
    public static void main(String[] args) {
        
        try {
            String genderFromPersonIDCode = Test.getGenderFromPersonIDCode("身份证号码");
            System.out.println(genderFromPersonIDCode);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
 
运行结果:
1:男  2:女


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值