• 通过身份号计算年龄,获取性别



    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Map;

    public class Test2 {

        public static void main(String[] args) {
            try {
                Test2 test2 = new Test2();
                test2.getCarInfo("身份证号");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
        /**
         * 根据身份证号计算年龄,获取性别
         * @param CardCode
         * @return
         * @throws Exception
         */
        public static Map<String, Object> getCarInfo(String CardCode)
                throws Exception {
            Map<String, Object> map = new HashMap<String, Object>();
    // 得到年份
            String year = CardCode.substring(6).substring(0, 4);
    // 得到月份
            String yue = CardCode.substring(10).substring(0, 2);
            String sex;
    // 判断性别
            if (Integer.parseInt(CardCode.substring(16).substring(0, 1)) % 2 == 0) {
                sex = "女";
            } else {
                sex = "男";
            }
            Date date = new Date();
    // 得到当前的系统时间
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    // 当前年份
            String fyear = format.format(date).substring(0, 4);
            String fyue = format.format(date).substring(5, 7);
    // 月份
            // String fday=format.format(date).substring(8,10);
            int age = 0;
    // 当前月份大于用户出身的月份表示已过生日
            if (Integer.parseInt(yue) <= Integer.parseInt(fyue)) {
                age = Integer.parseInt(fyear) - Integer.parseInt(year) + 1;
            } else {
                age = Integer.parseInt(fyear) - Integer.parseInt(year);
            }
            map.put("sex", sex);
            map.put("age", age);
            return map;
        }
    }

  • 相关阅读:
    Mybatis中javaType和jdbcType对应关系
    spy日志
    mybatis批量插入和更新
    js打印方案
    js弹窗,父子窗口调用
    extjs4.1
    oracle开启远程连接访问
    javaweb打印
    Leetcode 392.判断子序列
    Leetcode 391.完美矩形
  • 原文地址:https://www.cnblogs.com/nnnnmmmm/p/11661712.html
Copyright © 2020-2023  润新知