首页 > 其他分享 >好用的IdCardNumberMethod工具类(直接复制使用)

好用的IdCardNumberMethod工具类(直接复制使用)

时间:2024-07-09 15:44:27浏览次数:8  
标签:10 provinces idCard return String IdCardNumberMethod 复制 put 好用

package com.examine.ythgk.util;

import com.examine.common.core.utils.StringUtils;

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

/**
 * Created with IntelliJ IDEA.
 * User: Shendi
 * Date: 2022/12/4/004
 * Time: 17:04
 * Description:从身份证号码中获取出生日期、性别、年龄(15位和18位)
 */
public class IdCardNumberMethod {

    /**
     * 校验车牌号是否正确
     */
    public static boolean checkPlateNumber(String clcph){
        // 中国车牌号规则正则表达式,允许6或7位字符
        String pattern = "(^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳E]{1}[A-Z0-9]{1}$)|(^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$)";
        // 编译正则表达式
        Pattern p = Pattern.compile(pattern);
        // 进行匹配
        Matcher m = p.matcher(clcph);
        // 返回匹配结果
        return m.matches();
    }

    /**
     * 获取出生日期
     *
     * @return 返回字符串类型
     */
    public String getBirthFromIdCard(String idCard) {
        if (idCard.length() != 18 && idCard.length() != 15) {
            return "请输入正确的身份证号码";
        }
        if (idCard.length() == 18) {
            String year = idCard.substring(6).substring(0, 4);// 得到年份
            String month = idCard.substring(10).substring(0, 2);// 得到月份
            String day = idCard.substring(12).substring(0, 2);// 得到日
            return (year + "-" + month + "-" + day);
        } else if (idCard.length() == 15) {
            String year = "19" + idCard.substring(6, 8);// 年份
            String month = idCard.substring(8, 10);// 月份
            String day = idCard.substring(10, 12);// 得到日
            return (year + "-" + month + "-" + day);
        }
        return null;
    }

    /**
     * 获取出生日期
     *
     * @return 返回日期格式
     */
    public Date getBirthDayFromIdCard(String idCard) throws ParseException {
        Date birth = null;
        if (idCard.length() == 18) {
            String year = idCard.substring(6).substring(0, 4);// 得到年份
            String month = idCard.substring(10).substring(0, 2);// 得到月份
            String day = idCard.substring(12).substring(0, 2);// 得到日
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            birth = format.parse(year + "-" + month + "-" + day);
        } else if (idCard.length() == 15) {
            String year = "19" + idCard.substring(6, 8);// 年份
            String month = idCard.substring(8, 10);// 月份
            String day = idCard.substring(10, 12);// 得到日
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            birth = format.parse(year + "-" + month + "-" + day);
        }
        return birth;
    }

    /**
     * 获取性别
     *        0=未知的性别,9=未说明的性别,2=女性,1=男性
     * @return int
     */
    public static int getSexFromIdCard(String idCard) {
        int sex = 9;
        // 身份证号码为空
        if (idCard == "" || idCard.length() <= 0){
            return sex = 0;
        }
        if (idCard.length() == 18) {
            if (Integer.parseInt(idCard.substring(16).substring(0, 1)) % 2 == 0) {// 判断性别
                sex = 2; // 女
            } else {
                sex = 1; // 男
            }
        } else if (idCard.length() == 15) {
            String usex = idCard.substring(14, 15);// 用户的性别
            if (Integer.parseInt(usex) % 2 == 0) {
                sex = 2; // 女
            } else {
                sex = 1; // 男
            }
        }
        return sex;
    }
    /**
     * 获取性别
     *        0=未知的性别,9=未说明的性别,2=女性,1=男性
     * @return int
     */
    public static String getSexFromIdCardXbmc(String idCard) {
        String sex = "未说明的性别";
        // 身份证号码为空
        if (idCard == "" || idCard.length() <= 0){
            return sex = "未知的性别";
        }
        if (idCard.length() == 18) {
            if (Integer.parseInt(idCard.substring(16).substring(0, 1)) % 2 == 0) {// 判断性别
                sex = "女"; // 女
            } else {
                sex = "男"; // 男
            }
        } else if (idCard.length() == 15) {
            String usex = idCard.substring(14, 15);// 用户的性别
            if (Integer.parseInt(usex) % 2 == 0) {
                sex = "女"; // 女
            } else {
                sex = "男"; // 男
            }
        }
        return sex;
    }

    /**
     * 根据身份证的号码算出当前身份证持有者的年龄
     *
     * @param
     * @throws Exception
     * @return  -1(表示异常) 0 (身份证号码为空)
     */
    public static int getAgeForIdcard(String idcard) {
        try {
            int age = 0;
            if (StringUtils.isEmpty(idcard)) {
                return age;
            }

            String birth = "";
            if (idcard.length() == 18) {
                birth = idcard.substring(6, 14);
            } else if (idcard.length() == 15) {
                birth = "19" + idcard.substring(6, 12);
            }

            int year = Integer.valueOf(birth.substring(0, 4));
            int month = Integer.valueOf(birth.substring(4, 6));
            int day = Integer.valueOf(birth.substring(6));
            Calendar cal = Calendar.getInstance();
            age = cal.get(Calendar.YEAR) - year;
            //周岁计算
            if (cal.get(Calendar.MONTH) < (month - 1) || (cal.get(Calendar.MONTH) == (month - 1) && cal.get(Calendar.DATE) < day)) {
                age--;
            }
            return age;
        } catch (Exception e) {
            e.getMessage();
        }
        return -1;
    }

    /**
     * 15 位身份证号码转 18 位
     * <p>
     * 15位身份证号码:第7、8位为出生年份(两位数),第9、10位为出生月份,第11、12位代表出生日期,第15位代表性别,奇数为男,偶数为女。
     * 18位身份证号码:第7、8、9、10位为出生年份(四位数),第11、第12位为出生月份,第13、14位代表出生日期,第17位代表性别,奇数为男,偶数为女。
     */
    public StringBuffer IdCardMethod15To18(String idCard) {
        //将字符串转化为buffer进行操作
        StringBuffer stringBuffer = new StringBuffer(idCard);
        //身份证最后一位校验码,X代表10(顺序固定)
        char[] checkIndex = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
        int sum = 0;
        //在第6位插入年份的前两位19
        stringBuffer.insert(6, "19");
        for (int i = 0; i < stringBuffer.length(); i++) {
            char c = stringBuffer.charAt(i);
            //前17位数字
            int ai = Integer.valueOf(String.valueOf(c));
            //前17位每位对应的系数(7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2 )
            int wi = ((int) Math.pow(2, stringBuffer.length() - i)) % 11;
            //总和(每位数字乘以系数再相加)
            sum = sum + ai * wi;
        }
        //总和除以11求余
        int indexOf = sum % 11;
        //根据余数作为下表在校验码数组里取值
        stringBuffer.append(checkIndex[indexOf]);
        return stringBuffer;
    }

    /** 以下是对身份证号码验证内容 **/
    /** 省份开头字典 */
    private static Map<Integer, String> provinces = new HashMap<>();
    static {
        provinces.put(11, "北京");
        provinces.put(12, "天津");
        provinces.put(13, "河北");
        provinces.put(14, "山西");
        provinces.put(15, "内蒙古");
        provinces.put(21, "辽宁");
        provinces.put(22, "吉林");
        provinces.put(23, "黑龙江");
        provinces.put(31, "上海");
        provinces.put(32, "江苏");
        provinces.put(33, "浙江");
        provinces.put(34, "安徽");
        provinces.put(35, "福建");
        provinces.put(36, "江西");
        provinces.put(37, "山东");
        provinces.put(41, "河南");
        provinces.put(42, "湖北 ");
        provinces.put(43, "湖南");
        provinces.put(44, "广东");
        provinces.put(45, "广西");
        provinces.put(46, "海南");
        provinces.put(50, "重庆");
        provinces.put(51, "四川");
        provinces.put(52, "贵州");
        provinces.put(53, "云南");
        provinces.put(54, "西藏 ");
        provinces.put(61, "陕西");
        provinces.put(62, "甘肃");
        provinces.put(63, "青海");
        provinces.put(64, "宁夏");
        provinces.put(65, "新疆");
        provinces.put(71, "台湾");
        provinces.put(81, "香港");
        provinces.put(82, "澳门");
    }

    /**
     * @description 简单校验满足非空及18位,无法校验到月份对应天数
     * @date 2022/10/10 16:52
     * @param
     * @param IdCard
     * @return boolean
     */
    public static boolean easyCheckIdCard(String IdCard) {
        //字符串为空或者不符合简单的18位校验返回false
        String regex = "^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$";
        if (StringUtils.isBlank(IdCard) || !Pattern.matches(regex, IdCard)) {
            return false;
        }
        return true;
    }


    /**
     * @description 中级校验,不校验最后一位
     * @date 2022/10/10 16:57
     * @param
     * @param IdCard
     * @return boolean
     */
    public static boolean middleCheckIdCard(String IdCard) {
        return (easyCheckIdCard(IdCard) && checkBirthday(IdCard) && checkProvince(IdCard));
    }

    /**
     * @description 高级校验包含校验位
     * @date 2022/10/10 16:57
     * @param
     * @param IdCard
     * @return boolean
     */
    public static boolean highCheckIdCard(String IdCard) {
        return (middleCheckIdCard(IdCard) && checkLastNumber(IdCard));
    }

    /**
     * @description 身份证校验位的校验
     * @date 2022/10/10 15:16
     * @param
     * @param IdCard
     * @return boolean
     */
    public static boolean checkLastNumber(String IdCard) {
        try {
            char[] charArray = IdCard.toCharArray();
            //前十七位加权因子
            int[] IdCardWi = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
            //这是除以11后,可能产生的11位余数对应的验证码
            String[] IdCardY = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};
            int sum = 0;
            for (int i = 0; i < IdCardWi.length; i++) {
                int current = Integer.parseInt(String.valueOf(charArray[i]));
                int count = current * IdCardWi[i];
                sum += count;
            }
            char IdCardLast = charArray[17];
            int IdCardMod = sum % 11;
            return IdCardY[IdCardMod].toUpperCase().equals(String.valueOf(IdCardLast).toUpperCase());
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * @description 年月日校验
     * @date 2022/10/10 16:48
     * @param
     * @param IdCard
     * @return boolean
     */
    public static boolean checkBirthday(String IdCard) {
        String yearRegex = "(18|19|20)\\d{2}";
        String monthRegex = "(0[1-9])|10|11|12";
        String dayRegex = "([0-2][1-9])|10|20|30|31";
        String year = IdCard.substring(6, 10);
        if (!Pattern.matches(yearRegex, year)) {
            return false;
        }
        String month = IdCard.substring(10, 12);
        if (!Pattern.matches(monthRegex, month)) {
            return false;
        }
        String day = IdCard.substring(12, 14);
        if (!Pattern.matches(dayRegex, day)) {
            return false;
        }
        Integer yearInt = Integer.parseInt(year);
        Integer monthInt = Integer.parseInt(month);
        Integer dayInt = Integer.parseInt(day);
        switch (monthInt) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                return true;
            case 2:
                if (leapYear(yearInt)) {
                    //闰年 29
                    return dayInt <= 29;
                }
                return dayInt <= 28;
            default:
                return dayInt <= 30;
        }
    }

    /**
     * @description 校验是否为闰年
     * @date 2022/10/10 15:39
     * @param
     * @param year
     * @return boolean
     */
    public static boolean leapYear(int year) {
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
            return true;
        }
        return false;
    }

    /**
     * @description 身份证省份的校验,这里默认符合简单的18位校验
     * @date 2022/10/10 15:18
     * @param
     * @param IdCard
     * @return boolean
     */
    public static boolean checkProvince(String IdCard) {
        String pattern = "^[1-9][0-9]";
        String prov = IdCard.substring(0, 2);
        if (Pattern.matches(pattern, prov)) {
            return provinces.containsKey(Integer.parseInt(prov));
        }
        return false;
    }

}

 

标签:10,provinces,idCard,return,String,IdCardNumberMethod,复制,put,好用
From: https://www.cnblogs.com/shendidi/p/18292063

相关文章

  • 【MyBatis-Plus】 代码生成器使用指南——快速上手最好用的代码生成器!
    MyBatis-Plus代码生成器使用指南1.简介2.环境准备3.项目结构4.引入依赖5.编写代码生成器配置类6.配置解释6.1全局配置6.2数据源配置6.3包配置6.4模板配置6.5策略配置7.运行代码生成器8.生成的代码结构9.总结1.简介MyBatis-Plus是一个MyBatis......
  • Hadoop-17 Flume 介绍与环境配置 实机云服务器测试 分布式日志信息收集 海量数据 实时
    章节内容上一节我们完成了:HiveServer2的介绍和配置安装修改core-sizehdfs-site实现集群的启动Beeline简单上手HCatalog简单上手背景介绍这里是三台公网云服务器,每台2C4G,搭建一个Hadoop的学习环境,供我学习。之前已经在VM虚拟机上搭建过一次,但是没留下笔记,这次......
  • Redis复制过程详解
    主从复制简介  主从复制是为了达成高可用,即使有其中一台服务器宕机,其他服务器依然可以继续提供服务,实现Redis的高可用。  一个主节点可以有多个从节点(或没有从节点),但一个从节点只能有一个主节点。 主从复制的作用  读写分离:主节点写,从节点读,提高服务器的读写负载能......
  • Java面试八股之MySQL主从复制机制简述
    MySQL主从复制机制简述MySQL的主从复制机制是一种数据复制方案,用于在多个服务器之间同步数据。此机制允许从一个服务器(主服务器)到一个或多个其他服务器(从服务器)进行数据的复制,从而增强数据冗余、提高读取性能,并且为灾难恢复提供保障。以下是MySQL主从复制机制的简要概述:复制......
  • 2024年,值得收藏!推荐一些好用的数据库管理工具合集!
    数据库管理工具合集!1、DBeaver(首选)DBeaver是一款免费开源的跨平台数据库管理工具,基于Java开发,支持目前几乎所有的主流数据库,包括MySQL、PostgreSQL、SQLite、Oracle、SQLServer、DB2、Sybase、Teradata、MongoDB等。它具有直观的用户界面,支持SQL编辑、数据查看、数据编辑、元......
  • [oeasy]python024_vim读取文件_从头复制到尾_撤销_重做_reg_寄存器
    Guido的简历......
  • linux 下好用的 pdf 阅读器(不支持标注)
    安装,$sudoapt-getinstallzathura类似Vim的快捷键,普通浏览模式J:下翻一页K:上翻一页h,k,j,lCtrl+t,Ctrl+y:左右滚动半页Ctrl+d,Ctrl+u:上下滚动半页t,y:左右滚动一页Ctrl+f,Ctrl+b:上下滚动一页space,:上下滚动一页......
  • 数据恢复软件哪款好用?这 12 款专业数据恢复软件请收好!
    数据恢复软件哪款好用?在数字化时代,数据的重要性不言而喻。然而,由于各种原因,我们的数据可能会丢失或损坏,这时候就需要借助数据恢复软件来找回这些宝贵的信息。本文将为大家介绍12款专业数据恢复软件,帮助你在遇到数据丢失问题时,能够快速找到最适合自己的解决方案。数据恢复软件......
  • Redis主从复制实验
    实验环境系统:Linuxmaster(主库):192.168.1.2slave(从库):192.168.1.6两台服务器均安装好Redis数据库,安装步骤点这里开始搭建主从复制环境之前,确认防火墙是否开启,firewall-cmd--list-all实验步骤在slave下的配置进行配置,进入到/usr/local/bin下,viredis.conf配置redis.conf......
  • MIT6.824-2022 分布式系统课程实验笔记 Lab 2B Raft-日志复制(Log Replication)--xunznu
    Part2B:LogReplication日志复制(困难)文章目录Part2B:LogReplication日志复制(困难)Lab1:MapReduceLab2:Raft实验解读Lab2A:领导者选举leaderelection我的代码实现(附带详细代码注释)提示:实现细节:1、commitIndex和lastApplied2、nextIndex和matchIndex3、Co......