zoukankan      html  css  js  c++  java
  • JAVA通过身份证号码获取出生日期、年龄、性别

    JAVA验证身份证号码是否正确:https://www.cnblogs.com/pxblog/p/12038278.html

    /**
         * 通过身份证号码获取出生日期(birthday)、年龄(age)、性别(sex)
         * @param idCardNo 身份证号码
         * @return 返回的出生日期格式:1993-05-07   性别格式:1:男,0:女
         */
        public static Map<String, String> getBirthdayAgeSex(String idCardNo) {
            String birthday = "";
            String age = "";
            String sexCode = "";
    
            int year = Calendar.getInstance().get(Calendar.YEAR);
            char[] number = idCardNo.toCharArray();
            boolean flag = true;
            if (number.length == 15) {
                for (int x = 0; x < number.length; x++) {
                    if (!flag){
                        return new HashMap<String, String>();
                    }
                    flag = Character.isDigit(number[x]);
                }
            } else if (number.length == 18) {
                for (int x = 0; x < number.length - 1; x++) {
                    if (!flag){
                        return new HashMap<String, String>();
                    }
                    flag = Character.isDigit(number[x]);
                }
            }
            if (flag && idCardNo.length() == 15) {
                birthday = "19" + idCardNo.substring(6, 8) + "-"
                        + idCardNo.substring(8, 10) + "-"
                        + idCardNo.substring(10, 12);
                sexCode = Integer.parseInt(idCardNo.substring(idCardNo.length() - 3, idCardNo.length())) % 2 == 0 ? "0" : "1";
                age = (year - Integer.parseInt("19" + idCardNo.substring(6, 8))) + "";
            } else if (flag && idCardNo.length() == 18) {
                birthday = idCardNo.substring(6, 10) + "-"
                        + idCardNo.substring(10, 12) + "-"
                        + idCardNo.substring(12, 14);
                sexCode = Integer.parseInt(idCardNo.substring(idCardNo.length() - 4, idCardNo.length() - 1)) % 2 == 0 ? "0" : "1";
                age = (year - Integer.parseInt(idCardNo.substring(6, 10))) + "";
            }
            Map<String, String> map = new HashMap<String, String>();
            map.put("birthday", birthday);
            map.put("age", age);
            map.put("sex", sexCode);
            return map;
        }
  • 相关阅读:
    线性回归和逻辑回归
    行列式,叉积 (2)
    K最邻近算法(K-Nearest Neighbor,KNN)(初探)
    线性感知机和SVM(初探)
    向量点积(Dot Product)
    聚类(初探)
    sqlserver全文检索
    诗词背诵
    初级英语04
    初级英语03
  • 原文地址:https://www.cnblogs.com/pxblog/p/14275668.html
Copyright © 2011-2022 走看看