通过身份证号获取生日,年龄,性别的工具类

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.HashMap;

import java.util.Map;

/**

 * 身份证 算 年龄、性别

 *

 **/

public class IDCardsUtils {

    /**

     * 根据身份证的号码算出当前身份证持有者的性别和年龄 18位身份证

     *

     * @return

     * @throws Exception

     */

    public static Map<String, Object> getCarInfo(String CardCode) {

        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 day = CardCode.substring(12).substring(0, 2);//得到日

        map.put("birthday", year + "-" + yue + "-" + day);

        Integer sex;

        if (Integer.parseInt(CardCode.substring(16).substring(0, 1)) % 2 == 0) {// 判断性别

            sex = 0;

        } else {

            sex = 1;

        }

        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);

        Integer 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;

    }

 

    /**

     * 15位身份证的验证

     *

     * @param

     * @throws Exception

     */

    public static Map<String, Object> getCarInfo15W(String card) {

        Map<String, Object> map = new HashMap<String, Object>();

        String uyear = "19" + card.substring(6, 8);// 年份

        String uyue = card.substring(8, 10);// 月份

        // String uday=card.substring(10, 12);//

        String usex = card.substring(14, 15);// 用户的性别

        String sex;

        if (Integer.parseInt(usex) % 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(uyue) <= Integer.parseInt(fyue)) { // 当前月份大于用户出身的月份表示已过生

            age = Integer.parseInt(fyear) - Integer.parseInt(uyear) + 1;

        } else {// 当前用户还没过生

            age = Integer.parseInt(fyear) - Integer.parseInt(uyear);

        }

        map.put("sex", sex);

        map.put("age", age);

        return map;

    }

}

//调用方法

Map<String, Object> carInfo = IDCardsUtils.getCarInfo(user.getIdNum());
carInfo.get("sex");
carInfo.get("age");
carInfo.get("birthday");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值