身份证常用方法工具类

身份证常用方法工具类

package com.yutong.life.travel.controller;



import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: anxinjsb2
 * @Date: 2019-5-28
 * @Time: 17:59
 * @Description: 功能描述:身份证工具类
 */

public class IdCardUtils {
    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");
    private static String ID_NO_REGEX = "(^[1-9]\\d{5}(18|19|20|21)\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)";

    public IdCardUtils() {
    }

    /**
     * 获取年龄
     * @param idCard 身份证号
     * @return
     */
    public static int getAge(String idCard) {
        String yearMonthDay;
        if (getLength(idCard) == 15) {
            yearMonthDay = "19" + idCard.substring(6, 12);
        } else {
            if (getLength(idCard) != 18) {
                return 0;
            }

            yearMonthDay = idCard.substring(6, 14);
        }

        Calendar born = Calendar.getInstance();
        Calendar now = Calendar.getInstance();

        try {
            born.setTime(DATE_FORMAT.parse(yearMonthDay));
        } catch (ParseException var6) {
            return 0;
        }

        if (born.before(now)) {
            int age = now.get(1) - born.get(1);
            if (now.get(6) < born.get(6)) {
                --age;
            }

            return age;
        } else {
            return 0;
        }
    }

    /**
     * 判断是否满足最小年龄要求
     *
     * @param idCard 身份证号
     * @param ageDownLimit 最小年龄
     * @return
     */
    public static boolean checkAgeByIdNo(String idCard, int ageDownLimit) {
        if (idCard.length() == 18) {
            String idNoTempYear = idCard.substring(6, 10);
            String idNoTempMonth = idCard.substring(10, 12);
            String idNoTempDay = idCard.substring(12, 14);
            Integer idNoYear = Integer.parseInt(idNoTempYear);
            Integer idNoMonth = Integer.parseInt(idNoTempMonth);
            Integer idNoDay = Integer.parseInt(idNoTempDay);
            Calendar cal = Calendar.getInstance();
            int thisYear = cal.get(1);
            int thisMonth = cal.get(2);
            int thisDay = cal.get(5);
            int ageYear = thisYear - idNoYear;
            int ageMonth = thisMonth - idNoMonth + 1;
            int ageDay = thisDay - idNoDay;
            if (ageYear < ageDownLimit) {
                return false;
            } else {
                if (ageYear == ageDownLimit) {
                    if (ageMonth < 0) {
                        return false;
                    }

                    if (ageMonth == 0 && ageDay < 0) {
                        return false;
                    }
                }

                return true;
            }
        } else {
            return false;
        }
    }

    /**
     * 获取性别
     * @param idCard 身份证号
     * @return
     */
    public static String getSex(String idCard) {
        String strSexCode;
        if (getLength(idCard) == 15) {
            strSexCode = "19" + idCard.substring(14);
        } else {
            if (getLength(idCard) != 18) {
                return "";
            }

            strSexCode = idCard.substring(16, 17);
        }

        try {
            int sexCode = Integer.parseInt(strSexCode);
            String sex;
            if (sexCode % 2 == 0) {
                sex = "女";
            } else {
                sex = "男";
            }

            return sex;
        } catch (NumberFormatException var4) {
            return "";
        }
    }

    /**
     * 身份证号加密
     * @param idCard 身份证号
     * @return
     */
    public static String blur(String idCard) {
        StringBuffer blurIdCard = new StringBuffer();
        if (getLength(idCard) == 15) {
            blurIdCard.append(idCard.substring(0, 3));
            blurIdCard.append("********");
            blurIdCard.append(idCard.substring(idCard.length() - 4, idCard.length()));
        } else {
            if (getLength(idCard) != 18) {
                return idCard;
            }

            blurIdCard.append(idCard.substring(0, 3));
            blurIdCard.append("***********");
            blurIdCard.append(idCard.substring(idCard.length() - 4, idCard.length()));
        }

        return blurIdCard.toString();
    }

    private static int getLength(String idCard) {
        return idCard == null ? 0 : idCard.length();
    }

    /**
     * 判断当前身份号是否合法
     * @param idCard 身份证号
     * @return
     */
    public static boolean isIdCard(String idCard) {
        Pattern pattern = Pattern.compile(ID_NO_REGEX);
        Matcher isNum = pattern.matcher(idCard);
        return isNum.matches();
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值