package com.dsh.zealandweb.utils;

import java.util.HashSet;
import java.util.regex.Pattern; import org.apache.commons.lang3.StringUtils; public class IdCardUtil {
/** 大陆地区地域编码最大值 **/
public static final int MAX_MAINLAND_AREACODE = 659004;
/** 大陆地区地域编码最小值 **/
public static final int MIN_MAINLAND_AREACODE = 110000;
/** 香港地域编码值 **/
public static final int HONGKONG_AREACODE = 810000; // 香港地域编码值
/** 台湾地域编码值 **/
public static final int TAIWAN_AREACODE = 710000;
/** 澳门地域编码值 **/
public static final int MACAO_AREACODE = 820000; /** 数字正则 **/
public static final String regexNum = "^[0-9]*$";
/** 闰年生日正则 **/
public static final String regexBirthdayInLeapYear = "^((19[0-9]{2})|(200[0-9])|(201[0-5]))((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))$";
/** 平年生日正则 **/
public static final String regexBirthdayInCommonYear = "^((19[0-9]{2})|(200[0-9])|(201[0-5]))((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))$"; private static final HashSet<String> BLACK_SET = new HashSet<String>() { private static final long serialVersionUID = 48136604486603324L;
{
add("111111111111111");
}
}; /**
* <p>
* 身份证格式强校验
* </p>
* <p>
* 1、号码的结构 公民身份号码是特征组合码,由十七位数字本体码和一位校验码组成。排列顺序从左至右依次为:六位数字地址码,
* 八位数字出生日期码,三位数字顺序码和一位数字校验码。
* </p>
* <p>
* 2、地址码(前六位数)表示编码对象常住户口所在县(市、旗、区)的行政区划代码,按GB/T2260的规定执行。
* </p>
* <p>
* 3、出生日期码(第七位至十四位)表示编码对象出生的年、月、日,按GB/T7408的规定执行,年、月、日代码之间不用分隔符。
* </p>
* <p>
* 4、顺序码(第十五位至十七位)表示在同一地址码所标识的区域范围内,对同年、同月、同日出生的人编定的顺序号, 顺序码的奇数分配给男性,偶数分配给女性。
* </p>
* <p>
* 5、校验码(第十八位数)
* (1)十七位数字本体码加权求和公式 S = Sum(Ai * Wi), i = 0, ... , 16 ,先对前17位数字的权求和
* Ai:表示第i位置上的身份证号码数字值 Wi:表示第i位置上的加权因子 Wi: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4
* 2 (2)计算模 Y = mod(S, 11) (3)通过模得到对应的校验码 Y: 0 1 2 3 4 5 6 7 8 9 10 校验码: 1 0
* X 9 8 7 6 5 4 3 2
* </p>
*/
public static final boolean strongVerifyIdNumber(String idNumber) {
if (StringUtils.isBlank(idNumber)) {
return false;
}
idNumber = idNumber.trim(); if (BLACK_SET.contains(idNumber)) {
return false;
}
if (!checkIdNumberRegex(idNumber)) {
return false;
}
if (!checkIdNumberArea(idNumber.substring(0, 6))) {
return false;
}
idNumber = convertFifteenToEighteen(idNumber);
if (!checkBirthday(idNumber.substring(6, 14))) {
return false;
}
if (!checkIdNumberVerifyCode(idNumber)) {
return false;
}
return true;
} /**
* 身份证正则校验
*/
private static boolean checkIdNumberRegex(String idNumber) {
return Pattern.matches("^([0-9]{17}[0-9Xx])|([0-9]{15})$", idNumber);
} /**
* 身份证地区码检查
*/
private static boolean checkIdNumberArea(String idNumberArea) {
int areaCode = Integer.parseInt(idNumberArea);
if (areaCode == HONGKONG_AREACODE || areaCode == MACAO_AREACODE || areaCode == TAIWAN_AREACODE) {
return true;
}
if (areaCode <= MAX_MAINLAND_AREACODE && areaCode >= MIN_MAINLAND_AREACODE) {
return true;
}
return false;
} /**
* 将15位身份证转换为18位
*/
private static String convertFifteenToEighteen(String idNumber) {
if (15 != idNumber.length()) {
return idNumber;
}
idNumber = idNumber.substring(0, 6) + "19" + idNumber.substring(6, 15);
idNumber = idNumber + getVerifyCode(idNumber);
return idNumber;
} /**
* 根据身份证前17位计算身份证校验码
*/
private static String getVerifyCode(String idNumber) {
if (!Pattern.matches(regexNum, idNumber.substring(0, 17))) {
return null;
}
String[] ValCodeArr = { "1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2" };
String[] Wi = { "7", "9", "10", "5", "8", "4", "2", "1", "6", "3", "7", "9", "10", "5", "8", "4", "2" }; int sum = 0;
for (int i = 0; i < 17; i++) {
sum = sum + Integer.parseInt(String.valueOf(idNumber.charAt(i))) * Integer.parseInt(Wi[i]);
}
return ValCodeArr[sum % 11];
} /**
* 身份证出生日期嘛检查
*/
private static boolean checkBirthday(String idNumberBirthdayStr) {
Integer year = null;
try {
year = Integer.valueOf(idNumberBirthdayStr.substring(0, 4));
} catch (Exception e) {
}
if (null == year) {
return false;
}
if (isLeapYear(year)) {
return Pattern.matches(regexBirthdayInLeapYear, idNumberBirthdayStr);
} else {
return Pattern.matches(regexBirthdayInCommonYear, idNumberBirthdayStr);
}
} /**
* 判断是否为闰年
*/
private static boolean isLeapYear(int year) {
return (year % 400 == 0) || (year % 100 != 0 && year % 4 == 0);
} /**
* 身份证校验码检查
*/
private static boolean checkIdNumberVerifyCode(String idNumber) {
return getVerifyCode(idNumber).equalsIgnoreCase(idNumber.substring(17));
} public static void main(String[] args) {
System.out.println(strongVerifyIdNumber("111111111111111"));
}
}

转自:https://blog.csdn.net/zhengyong15984285623/article/details/51784731

最新文章

  1. 虚拟机下CentOS 6.5配置IP地址的三种方法
  2. Ionic2学习笔记(1):新建一个页面
  3. strcpy之代码的健壮性与可维护性
  4. 进击的java - tomcat的安装,配置都正确之后,还是报错
  5. 2016年11月3日 星期四 --出埃及记 Exodus 19:19
  6. MySQL Cluster 4个数据节点压力测试--mysqlslap工具压400W写
  7. WCF Test Client
  8. call_grant_exec.sql
  9. 栈&amp;队列
  10. SQL SERVER 2008 服务器登录名、角色、数据库用户、角色、架构的关系
  11. was类加载器
  12. 固定div的位置——不随窗口大小改变为改变位置
  13. Android studio 编译出现的问题记录
  14. tensorflow 批次读取文件内的数据,并将顺序随机化处理. --[python]
  15. ML.NET教程之情感分析(二元分类问题)
  16. thinkphp3.1.3导入
  17. 系统启动时队列自动下单--ServletContextListener
  18. Linux IO模式-阻塞io、非阻塞io、多路复用io
  19. 我的SQL里哪个语句占用的CPU最多?
  20. SparkR链接mysql数据库(踩坑)

热门文章

  1. Java常用类库(一) : Object 和日期类的简单使用
  2. CSS知识点整理(2):框模型,定位
  3. Android 使用WebView浏览有声音或者视频的网页,关闭WebView之后,声音或者视频不停止的解决办法
  4. html5——3D案例(音乐盒子、百度钱包)
  5. JS——indexOf replace search
  6. SQL基本操作——函数
  7. CentOS7配置VSFTP服务器
  8. HDU_1502_dp
  9. 浏览器的 local storage
  10. 关于 实体类中 时间字段 为string 类型和 datatime类型 比较