二话不说,直接上代码。

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实现身份证号码校验的更多相关文章

  1. JAVA验证身份证号码是否正确

    package com.IdCard; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.D ...

  2. JAVA通过身份证号码获取出生日期、年龄、性别

    JAVA验证身份证号码是否正确:https://www.cnblogs.com/pxblog/p/12038278.html /** * 通过身份证号码获取出生日期(birthday).年龄(age) ...

  3. 【转】身份证号码校验与信息提取 - Java 代码

    转载地址:http://www.w3china.org/blog/more.asp?name=lhwork&id=19148 import java.util.regex.*;   /**   ...

  4. 【Java】身份证号码验证

    代码引用自:https://gitee.com/appleat/codes/ynrtqujv0wfgesm8ia9b547 package xxx; /** * Created by wdj on 2 ...

  5. JAVA验证身份证号码是否合法

    package com.chauvet.utils; import java.text.ParseException; import java.text.SimpleDateFormat; impor ...

  6. Java实现身份证号码验证源码分享

    import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; impor ...

  7. Java的身份证号码工具类

    /** * Copyright (C) 2009-2010 Yichuan, Fuchun All rights reserved. * Licensed to the Apache Software ...

  8. java验证身份证号码是否有效源代码 wn25的头像 wn25 23 2015-01-04 20:09 6 基本信息 Java × 1 浏览

    原文:http://www.open-open.com/code/view/1420373343171 1.描述 用java语言判断身份证号码是否有效,地区码.出身年月.校验码等验证算法 2.源代码 ...

  9. java验证身份证号码是否有效源代码

    原文:http://www.open-open.com/code/view/1420373343171 1.描述 用java语言判断身份证号码是否有效,地区码.出身年月.校验码等验证算法 2.源代码 ...

随机推荐

  1. System.IO 二

    接着上篇的来  System.IO FileSystemWatcher    指向这个签名的方法   可以监听目录发生了什么事件 例如: static void Main(string[] args) ...

  2. linux下Mycat的安装配置

    1.下载Mycat Linux版:下载链接 2.通过SSH直连工具把安装包丢到linux:/usr/local/ 3.解压安装Mycat 4.配置环境 5.使配置文件生效

  3. Webpack4 学习笔记三 ES6+语法降级为ES5

    前言 此内容是个人学习笔记,以便日后翻阅.非教程,如有错误还请指出 Webpack 将es6.es7语法降级为es5 需要通过 babel JavaScript编译器. 安装: npm i babel ...

  4. CBCGPImage的GetSize的问题及解决方法

    BCGControlBar Pro for MFC 25.10是目前(2018-07-16)网上能够找到的最新能够使用的版本,我配合Visual Studio 2010使用.在单文档MFC程序的视图中 ...

  5. Linux关闭开启防火墙命令

    在外部访问CentOS中部署应用时,需要关闭防火墙. 关闭防火墙命令:systemctl stop firewalld.service 开启防火墙:systemctl start firewalld. ...

  6. 分析setup/hold电气特性从D触发器内部结构角度

    上图是用与非门实现的D触发器的逻辑结构图,CP是时钟信号输入端,S和R分别是置位和清零信号,低有效; D是信号输入端,Q信号输出端; 这里先说一下D触发器实现的原理:(假设S和R信号均为高,不进行置位 ...

  7. tcl之过程/函数-proc

  8. SSO 单点登录总结(PHP)

    本篇文章根据个人理解的知识整理汇总,如有不足之处,请大家多多指正. 单点登录(SSO--Single Sign On)的应用是很普遍的,尤其在大型网站系统中,比如百度,登录百度账号和,再转到百度经验. ...

  9. ios 苹果内购订单验证 --- nodejs实现

    实现代码 function IosPlayVerify(data,orderid,cb) { itunesPost(data,function (error,responseData) { if (e ...

  10. PLC状态机编程第一篇-状态机介绍

    状态机的一般概念 过去我在学习PLC编程时,看的大部分书仅仅停留在软件的操作上,没有真正讲述如何组织程序,这里我们讲一个通用的描述控制算法的方法,就是状态机,和SFC很类似,其实SFC也是源于状态机啦 ...