二话不说,直接上代码。

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. C#中 property 与 attribute的区别?

    C#中 property 与 attribute的区别?答:attribute:自定义属性的基类;property :类中的属性

  2. Spring框架进阶3

    Spring框架进阶3 测试spring_jdbc和spring对事务的管理 先配置相应的pom <?xml version="1.0" encoding="UTF ...

  3. spring-mybatis整合项目 异常处理2

    org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'com/imooc ...

  4. java.lang.UnsupportedOperationException 原因以及解决方案

    如下代码: Map[] cardProds = JsonUtils.getObject(oldCartValue, new TypeReference<Map[]>(){}); List& ...

  5. 搭建一个简单的dns缓存服务器

    环境:linux 软件:bind97,bind97-utils, bind97-libs ip:192.168.192.130:192.168.192.131 -------------------- ...

  6. 最短路径之迪杰斯特拉算法(Java)

    1)Dijkstra算法适用于求图中两节点之间最短路径 2)Dijkstra算法设计比较巧妙的是:在求源节点到终结点自底向上的过程中,源节点到某一节点之间最短路径的确定上(这也是我之前苦于没有解决的地 ...

  7. iOS常用控件-UITableViewCell

    一. 封装cell: 1.加载xib文件的两种方式 <方式1> (NewsCell是xib文件的名称) NSArray *objects = [[NSBundle mainBundle] ...

  8. Educational Codeforces Round 43 E. Well played!(贪心)

    E. Well played! time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  9. POJ:2449-Remmarguts' Date(单源第K短路)

    Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 33081 Accepted: 8993 Des ...

  10. [Hdu4825]Xor Sum(01字典树)

    Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeus 发起M次询问 ...