首页 / JAVA / Java实现身份证号码校验
Java实现身份证号码校验
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java实现身份证号码校验,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1617字,纯文字阅读大概需要3分钟。
内容图文
二话不说,直接上代码。
/** * 校验18位身份证号 * * @param identityCode * * 返回true则表示校验通过 */ public boolean checkIdentityCode(String identityCode) { // 校验身份证位数为18位 if (!identityCode.matches("\\d{17}(\\d|x|X)$")) { returnfalse; } 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-当前年份之间returnfalse; } if (Integer.parseInt(identityCode.substring(10, 12)) < 1 || Integer.parseInt(identityCode.substring(10, 12)) > 12) {// 11-12位代表出生月份,范围应该在01-12之间returnfalse; } if (Integer.parseInt(identityCode.substring(12, 14)) < 1 || Integer.parseInt(identityCode.substring(12, 14)) > 31) {// 13-14位是出生日期,范围应该在01-31之间returnfalse; } // 校验第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 = newint[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位校验码错误returnfalse; } returntrue; }
原文:http://www.cnblogs.com/orionhp/p/6702544.html
内容总结
以上是互联网集市为您收集整理的Java实现身份证号码校验全部内容,希望文章能够帮你解决Java实现身份证号码校验所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。