Java实现身份证号码校验
二话不说,直接上代码。
package hope.identitycodecheck.demo;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
*
* @author hp
*
* */
public class IdentityCodeUtil {
/**
* 身份证号校验 (支持18位)
*
* */
public static boolean checkIdentityCode(String identityCode) {
if (!identityCode.matches("\\d{17}(\\d|x|X)$")) {
return false;
}
Date d = new Date();
DateFormat df = new SimpleDateFormat("yyyyMMdd");
int year = Integer.parseInt(df.format(d));
if (Integer.parseInt(identityCode.substring(6, 10)) < 1900 || Integer.parseInt(identityCode.substring(6, 10)) > year) {// 7-10位是出生年份,范围应该在1900-当前年份之间
return false;
}
if (Integer.parseInt(identityCode.substring(10, 12)) < 1 || Integer.parseInt(identityCode.substring(10, 12)) > 12) {// 11-12位代表出生月份,范围应该在01-12之间
return false;
}
if (Integer.parseInt(identityCode.substring(12, 14)) < 1 || Integer.parseInt(identityCode.substring(12, 14)) > 31) {// 13-14位是出生日期,范围应该在01-31之间
return false;
}
// 校验第18位
// 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
String[] tempA = identityCode.split("|");
int[] a = new int[18];
for (int i = 0; i < tempA.length - 2; i++) {
a[i] = Integer.parseInt(tempA[i + 1]);
}
int[] w = { 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 + a[i] * w[i];
}
// Y = mod(S, 11)
// 通过模得到对应的校验码
// Y: 0 1 2 3 4 5 6 7 8 9 10
// 校验码: 1 0 X 9 8 7 6 5 4 3 2
String[] v = { "1", "0", "x", "9", "8", "7", "6", "5", "4", "3", "2" }; // 校验码
int y = sum % 11;
if (!v[y].equalsIgnoreCase(identityCode.substring(17))) {// 第18位校验码错误
return false;
}
return true;
} public static void main(String[] args) {
System.out.println(checkIdentityCode("110110198001019719"));
}
}
Java实现身份证号码校验的更多相关文章
- JAVA验证身份证号码是否正确
package com.IdCard; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.D ...
- JAVA通过身份证号码获取出生日期、年龄、性别
JAVA验证身份证号码是否正确:https://www.cnblogs.com/pxblog/p/12038278.html /** * 通过身份证号码获取出生日期(birthday).年龄(age) ...
- 【转】身份证号码校验与信息提取 - Java 代码
转载地址:http://www.w3china.org/blog/more.asp?name=lhwork&id=19148 import java.util.regex.*; /** ...
- 【Java】身份证号码验证
代码引用自:https://gitee.com/appleat/codes/ynrtqujv0wfgesm8ia9b547 package xxx; /** * Created by wdj on 2 ...
- JAVA验证身份证号码是否合法
package com.chauvet.utils; import java.text.ParseException; import java.text.SimpleDateFormat; impor ...
- Java实现身份证号码验证源码分享
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; impor ...
- Java的身份证号码工具类
/** * Copyright (C) 2009-2010 Yichuan, Fuchun All rights reserved. * Licensed to the Apache Software ...
- java验证身份证号码是否有效源代码 wn25的头像 wn25 23 2015-01-04 20:09 6 基本信息 Java × 1 浏览
原文:http://www.open-open.com/code/view/1420373343171 1.描述 用java语言判断身份证号码是否有效,地区码.出身年月.校验码等验证算法 2.源代码 ...
- java验证身份证号码是否有效源代码
原文:http://www.open-open.com/code/view/1420373343171 1.描述 用java语言判断身份证号码是否有效,地区码.出身年月.校验码等验证算法 2.源代码 ...
随机推荐
- C#静态成员和非静态成员
一.C#静态成员和非静态成员 1. C#静态成员和非静态成员 当类中的某个成员使用static修饰符时,就会被声明为静态成员.类中的成员要么是静态成员,要么是非静态成员.一般情况下,静态成员属于整个类 ...
- iRate快速绕坑使用
目的 iRate库通过激励用户去AppStore打分,来帮助你提升iPhone和Mac App的质量.这是取得经常使用的目标用户的意见的最好的方式之一. 方案(小弟想说的重点) 以前,App中都是显示 ...
- BZOJ1202: [HNOI2005]狡猾的商人(带权并查集)
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4577 Solved: 2249[Submit][Status][Discuss] Descript ...
- SAP BI 常用TCODE
S.No Tcode Description 1 RSA1 Administrator Work Bench 2 RSA11 Calling up AWB with the IC tree 3 RSA ...
- MySQL中事物的详解
1. 事物的定义及特性 事务是一组操作数据库的SQL语句组成的工作单元,该工作单元中所有操作要么同时成功,要么同时失败.事物有如下四个特性,ACID简称“酸性”. 1)原子性:工作单元中所有的操作要么 ...
- linux特殊权限位suid
特殊权限位基本说明(了解): linux系统基本权限位为9位权限,但还有额外3位权限位,共12位权限: suid s(x) S 4 用户对应的权限位(用户对应的3位 ...
- 多进程(multiprocessing module)
一.多进程 1.1 多进程的概念 由于GIL的存在,python中的多线程其实并不是真正的多线程,如果想要充分地使用多核CPU的资源,在python中大部分情况需要使用多进程.Python提供了非常好 ...
- python__高级 : Property 的使用
一个类中,假如一个私有属性,有两个方法,一个是getNum , 一个是setNum 它,那么可以用 Property 来使这两个方法结合一下,比如这样用 num = property(getNum, ...
- Educational Codeforces Round 37 E. Connected Components?(图论)
E. Connected Components? time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- [CodeForces940E]Cashback(set+DP)
Description Since you are the best Wraith King, Nizhniy Magazin «Mir» at the centre of Vinnytsia is ...