JAVA验证身份证号码是否正确
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | package com.IdCard; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.Map; public class ValidateIdCardUtil { final static Map<Integer, String> zoneNum = new HashMap<Integer, String>(); static { zoneNum.put( 11 , "北京" ); zoneNum.put( 12 , "天津" ); zoneNum.put( 13 , "河北" ); zoneNum.put( 14 , "山西" ); zoneNum.put( 15 , "内蒙古" ); zoneNum.put( 21 , "辽宁" ); zoneNum.put( 22 , "吉林" ); zoneNum.put( 23 , "黑龙江" ); zoneNum.put( 31 , "上海" ); zoneNum.put( 32 , "江苏" ); zoneNum.put( 33 , "浙江" ); zoneNum.put( 34 , "安徽" ); zoneNum.put( 35 , "福建" ); zoneNum.put( 36 , "江西" ); zoneNum.put( 37 , "山东" ); zoneNum.put( 41 , "河南" ); zoneNum.put( 42 , "湖北" ); zoneNum.put( 43 , "湖南" ); zoneNum.put( 44 , "广东" ); zoneNum.put( 45 , "广西" ); zoneNum.put( 46 , "海南" ); zoneNum.put( 50 , "重庆" ); zoneNum.put( 51 , "四川" ); zoneNum.put( 52 , "贵州" ); zoneNum.put( 53 , "云南" ); zoneNum.put( 54 , "西藏" ); zoneNum.put( 61 , "陕西" ); zoneNum.put( 62 , "甘肃" ); zoneNum.put( 63 , "青海" ); zoneNum.put( 64 , "宁夏" ); zoneNum.put( 65 , "新疆" ); zoneNum.put( 71 , "台湾" ); zoneNum.put( 81 , "香港" ); zoneNum.put( 82 , "澳门" ); zoneNum.put( 91 , "外国" ); } final static int [] PARITYBIT = { '1' , '0' , 'X' , '9' , '8' , '7' , '6' , '5' , '4' , '3' , '2' }; final static int [] POWER_LIST = { 7 , 9 , 10 , 5 , 8 , 4 , 2 , 1 , 6 , 3 , 7 , 9 , 10 , 5 , 8 , 4 , 2 }; /** * 身份证验证 * * @param certNo 号码内容 * @return 是否有效 null和"" 都是false */ public static boolean isIDCard(String certNo) { if (certNo == null || (certNo.length() != 15 && certNo.length() != 18 )) return false ; final char [] cs = certNo.toUpperCase().toCharArray(); //校验位数 int power = 0 ; for ( int i = 0 ; i < cs.length; i++) { if (i == cs.length - 1 && cs[i] == 'X' ) break ; //最后一位可以 是X或x if (cs[i] < '0' || cs[i] > '9' ) return false ; if (i < cs.length - 1 ) { power += (cs[i] - '0' ) * POWER_LIST[i]; } } //校验区位码 if (!zoneNum.containsKey(Integer.valueOf(certNo.substring( 0 , 2 )))) { return false ; } //校验年份 String year = null ; year = certNo.length() == 15 ? getIdcardCalendar(certNo) : certNo.substring( 6 , 10 ); final int iyear = Integer.parseInt(year); if (iyear < 1900 || iyear > Calendar.getInstance().get(Calendar.YEAR)) return false ; //1900年的PASS,超过今年的PASS //校验月份 String month = certNo.length() == 15 ? certNo.substring( 8 , 10 ) : certNo.substring( 10 , 12 ); final int imonth = Integer.parseInt(month); if (imonth < 1 || imonth > 12 ) { return false ; } //校验天数 String day = certNo.length() == 15 ? certNo.substring( 10 , 12 ) : certNo.substring( 12 , 14 ); final int iday = Integer.parseInt(day); if (iday < 1 || iday > 31 ) return false ; //校验"校验码" if (certNo.length() == 15 ) return true ; return cs[cs.length - 1 ] == PARITYBIT[power % 11 ]; } private static String getIdcardCalendar(String certNo) { // 获取出生年月日 String birthday = certNo.substring( 6 , 12 ); SimpleDateFormat ft = new SimpleDateFormat( "yyMMdd" ); Date birthdate = null ; try { birthdate = ft.parse(birthday); } catch (java.text.ParseException e) { e.printStackTrace(); } Calendar cday = Calendar.getInstance(); cday.setTime(birthdate); String year = String.valueOf(cday.get(Calendar.YEAR)); return year; } public static void main(String[] args) { boolean istrue = isIDCard( "330622810725323" ); System.out.println(istrue); } } |
JAVA通过身份证号码获取出生日期、年龄、性别 https://www.cnblogs.com/pxblog/p/14275668.html
-----------------------有任何问题可以在评论区评论,也可以私信我,我看到的话会进行回复,欢迎大家指教------------------------
(蓝奏云官网有些地址失效了,需要把请求地址lanzous改成lanzoux才可以)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了