• java 通过身份证号码获取出生日期、性别、年龄


    工具类1

    /**
     * Created by Administrator on 2016/2/25.
     */
    public class IdCard {
        /** 中国公民身份证号码最小长度。 */
        public  final int CHINA_ID_MIN_LENGTH = 15;
    
        /** 中国公民身份证号码最大长度。 */
        public  final int CHINA_ID_MAX_LENGTH = 18;
        /**
         * 根据身份编号获取年龄
         *
         * @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 性别(M-男,F-女,N-未知)
         */
        public static String getGenderByIdCard(String idCard) {
            String sGender = "未知";
    
            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<Wcbxx> 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 "";
        }*/
    
        @Test
        public static  void  main(String [] a){
            String idcard="460200199209275127";
            String sex= getGenderByIdCard(idcard);
            System.out.println("性别:" + sex);
            int age= getAgeByIdCard(idcard);
            System.out.println("年龄:" + age);
            Short nian=getYearByIdCard(idcard);
            Short yue=getMonthByIdCard(idcard);
            Short ri=getDateByIdCard(idcard);
            System.out.print(nian+"年"+yue+"月"+ri+"日");
    
            String sr=getBirthByIdCard(idcard);
            System.out.println("生日:" + sr);
        }
    
    }

    工具类2

      /**
         * 通过身份证号码获取出生日期、性别、年龄
         * @param certificateNo
         * @return 返回的出生日期格式:1990-01-01 性别格式:F-女,M-男
         */
        public static Map<String, String> getBirAgeSex(String certificateNo) {
            String birthday = "";
            String age = "";
            String sexCode = "";
    
            int year = Calendar.getInstance().get(Calendar.YEAR);
            char[] number = certificateNo.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 && certificateNo.length() == 15) {
                birthday = "19" + certificateNo.substring(6, 8) + "-" + certificateNo.substring(8, 10) + "-"
                        + certificateNo.substring(10, 12);
                sexCode = Integer.parseInt(certificateNo.substring(certificateNo.length() - 3, certificateNo.length()))
                        % 2 == 0 ? "F" : "M";
                age = (year - Integer.parseInt("19" + certificateNo.substring(6, 8))) + "";
            } else if (flag && certificateNo.length() == 18) {
                birthday = certificateNo.substring(6, 10) + "-" + certificateNo.substring(10, 12) + "-"
                        + certificateNo.substring(12, 14);
                sexCode = Integer.parseInt(certificateNo.substring(certificateNo.length() - 4, certificateNo.length() - 1))
                        % 2 == 0 ? "F" : "M";
                age = (year - Integer.parseInt(certificateNo.substring(6, 10))) + "";
            }
            Map<String, String> map = new HashMap<String, String>();
            map.put("birthday", birthday);
            map.put("age", age);
            map.put("sexCode", sexCode);
            return map;
        }
  • 相关阅读:
    字符串----不可重叠的最长重复子串
    字符串----最长重复子串
    字符串----HDU-1358
    字符串----hiho字符串(尺取法)
    字符串匹配(二)----KMP算法
    字符串匹配(一)----Rabin-Karp算法
    字符串----最短摘要生成(尺取法)
    【Hibernate 检索策略】
    【Hibernate 多表查询】
    【Hibernate QBC】
  • 原文地址:https://www.cnblogs.com/shanheyongmu/p/13409298.html
Copyright © 2020-2023  润新知