Android 自用身份证号码校验工具类
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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 | /** * 身份证验证 * Created by tab on 2019/04/04 */ public class IdCardUtil { private String idCardNum = null ; private static int IS_EMPTY = 1 ; private static int LEN_ERROR = 2 ; private static int CHAR_ERROR = 3 ; private static int DATE_ERROR = 4 ; private static int CHECK_BIT_ERROR = 5 ; private String[] errMsg = new String[]{ "身份证完全正确!" , "身份证为空!" , "身份证长度不正确!" , "身份证有非法字符!" , "身份证中出生日期不合法!" , "身份证校验位错误!" }; private int error = 0 ; /** * 构造方法。 * * @param idCardNum */ public IdCardUtil(String idCardNum) { // super(); this .idCardNum = idCardNum.trim(); if (!TextUtils.isEmpty( this .idCardNum)) { this .idCardNum = this .idCardNum.replace( "x" , "X" ); } } public String getIdCardNum() { return idCardNum; } public void setIdCardNum(String idCardNum) { this .idCardNum = idCardNum; if (!TextUtils.isEmpty( this .idCardNum)) { this .idCardNum = this .idCardNum.replace( "x" , "X" ); } } /** * 得到身份证详细错误信息。 * * @return 错误信息。 */ public String getErrMsg() { return this .errMsg[ this .error]; } /** * 是否为空。 * * @return true: null false: not null; */ public boolean isEmpty() { if ( this .idCardNum == null ) return true ; else return this .idCardNum.trim().length() > 0 ? false : true ; } /** * 身份证长度。 * * @return */ public int getLength() { return this .isEmpty() ? 0 : this .idCardNum.length(); } /** * 身份证长度。 * * @return */ public int getLength(String str) { return this .isEmpty() ? 0 : str.length(); } /** * 是否是15位身份证。 * * @return true: 15位 false:其他。 */ public boolean is15() { return this .getLength() == 15 ; } /** * 是否是18位身份证。 * * @return true: 18位 false:其他。 */ public boolean is18() { return this .getLength() == 18 ; } /** * 得到身份证的省份代码。 * * @return 省份代码。 */ public String getProvince() { return this .isCorrect() == 0 ? this .idCardNum.substring( 0 , 2 ) : "" ; } /** * 得到身份证的城市代码。 * * @return 城市代码。 */ public String getCity() { return this .isCorrect() == 0 ? this .idCardNum.substring( 2 , 4 ) : "" ; } /** * 得到身份证的区县代码。 * * @return 区县代码。 */ public String getCountry() { return this .isCorrect() == 0 ? this .idCardNum.substring( 4 , 6 ) : "" ; } /** * 得到身份证的出生年份。 * * @return 出生年份。 */ public String getYear() { if ( this .isCorrect() != 0 ) return "" ; if ( this .getLength() == 15 ) { return "19" + this .idCardNum.substring( 6 , 8 ); } else { return this .idCardNum.substring( 6 , 10 ); } } /** * 得到身份证的出生月份。 * * @return 出生月份。 */ public String getMonth() { if ( this .isCorrect() != 0 ) return "" ; if ( this .getLength() == 15 ) { return this .idCardNum.substring( 8 , 10 ); } else { return this .idCardNum.substring( 10 , 12 ); } } /** * 得到身份证的出生日子。 * * @return 出生日期。 */ public String getDay() { if ( this .isCorrect() != 0 ) return "" ; if ( this .getLength() == 15 ) { return this .idCardNum.substring( 10 , 12 ); } else { return this .idCardNum.substring( 12 , 14 ); } } /** * 得到身份证的出生日期。 * * @return 出生日期。 */ public String getBirthday() { if ( this .isCorrect() != 0 ) return "" ; if ( this .getLength() == 15 ) { return "19" + this .idCardNum.substring( 6 , 12 ); } else { return this .idCardNum.substring( 6 , 14 ); } } /** * 得到身份证的出生年月。 * * @return 出生年月。 */ public String getBirthMonth() { return getBirthday().substring( 0 , 6 ); } /** * 得到身份证的顺序号。 * * @return 顺序号。 */ public String getOrder() { if ( this .isCorrect() != 0 ) return "" ; if ( this .getLength() == 15 ) { return this .idCardNum.substring( 12 , 15 ); } else { return this .idCardNum.substring( 14 , 17 ); } } /** * 得到性别。 * * @return 性别:1-男 2-女 */ public String getSex() { if ( this .isCorrect() != 0 ) return "" ; int p = Integer.parseInt(getOrder()); if (p % 2 == 1 ) { return "男" ; } else { return "女" ; } } /** * 得到性别值。 * * @return 性别:1-男 2-女 */ public String getSexValue() { if ( this .isCorrect() != 0 ) return "" ; int p = Integer.parseInt(getOrder()); if (p % 2 == 1 ) { return "1" ; } else { return "2" ; } } /** * 得到校验位。 * * @return 校验位。 */ public String getCheck() { if (! this .isLenCorrect()) return "" ; String lastStr = this .idCardNum.substring( this .idCardNum.length() - 1 ); if ( "x" .equals(lastStr)) { lastStr = "X" ; } return lastStr; } /** * 得到15位身份证。 * * @return 15位身份证。 */ public String to15() { if ( this .isCorrect() != 0 ) return "" ; if ( this .is15()) return this .idCardNum; else return this .idCardNum.substring( 0 , 6 ) + this .idCardNum.substring( 8 , 17 ); } /** * 得到18位身份证。 * * @return 18位身份证。 */ public String to18() { if ( this .isCorrect() != 0 ) return "" ; if ( this .is18()) return this .idCardNum; else return this .idCardNum.substring( 0 , 6 ) + "19" + this .idCardNum.substring( 6 ) + this .getCheckBit(); } /** * 得到18位身份证。 * * @return 18位身份证。 */ public static String toNewIdCard(String tempStr) { if (tempStr.length() == 18 ) return tempStr.substring( 0 , 6 ) + tempStr.substring( 8 , 17 ); else return tempStr.substring( 0 , 6 ) + "19" + tempStr.substring( 6 ) + getCheckBit(tempStr); } /** * 校验身份证是否正确 * * @return 0:正确 */ public int isCorrect() { try { if ( this .isEmpty()) { this .error = IdCardUtil.IS_EMPTY; return this .error; } if (! this .isLenCorrect()) { this .error = IdCardUtil.LEN_ERROR; return this .error; } if (! this .isCharCorrect()) { this .error = IdCardUtil.CHAR_ERROR; return this .error; } if (! this .isDateCorrect()) { this .error = IdCardUtil.DATE_ERROR; return this .error; } if ( this .is18()) { if (! this .getCheck().equals( this .getCheckBit())) { this .error = IdCardUtil.CHECK_BIT_ERROR; return this .error; } } } catch (Exception e) { ULogger.e(e); this .error = IdCardUtil.CHECK_BIT_ERROR; return this .error; } return 0 ; } private boolean isLenCorrect() { return this .is15() || this .is18(); } /** * 判断身份证中出生日期是否正确。 * * @return */ private boolean isDateCorrect() { /*非闰年天数*/ int [] monthDayN = { 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 }; /*闰年天数*/ int [] monthDayL = { 31 , 29 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 }; int month; if ( this .is15()) { month = UtilsString.StringToInteger( this .idCardNum.substring( 8 , 10 )); } else { month = UtilsString.StringToInteger( this .idCardNum.substring( 10 , 12 )); } int day; if ( this .is15()) { day = UtilsString.StringToInteger( this .idCardNum.substring( 10 , 12 )); } else if ( this .is18()) { day = UtilsString.StringToInteger( this .idCardNum.substring( 12 , 14 )); } else { return false ; } if (month > 12 || month <= 0 ) { return false ; } if ( this .isLeapyear()) { if (day > monthDayL[month - 1 ] || day <= 0 ) return false ; } else { if (day > monthDayN[month - 1 ] || day <= 0 ) return false ; } return true ; } /** * 得到校验位。 * * @return */ private String getCheckBit() { if (! this .isLenCorrect()) return "" ; String temp = null ; if ( this .is18()) temp = this .idCardNum; else temp = this .idCardNum.substring( 0 , 6 ) + "19" + this .idCardNum.substring( 6 ); String checkTable[] = new String[]{ "1" , "0" , "X" , "9" , "8" , "7" , "6" , "5" , "4" , "3" , "2" }; int [] wi = new int []{ 7 , 9 , 10 , 5 , 8 , 4 , 2 , 1 , 6 , 3 , 7 , 9 , 10 , 5 , 8 , 4 , 2 , 1 }; int sum = 0 ; for ( int i = 0 ; i < 17 ; i++) { String ch = temp.substring(i, i + 1 ); sum = sum + UtilsString.StringToInteger(ch) * wi[i]; } int y = sum % 11 ; return checkTable[y]; } /** * 得到校验位。 * * @return */ private static String getCheckBit(String str) { String temp = null ; if (str.length() == 18 ) temp = str; else temp = str.substring( 0 , 6 ) + "19" + str.substring( 6 ); String checkTable[] = new String[]{ "1" , "0" , "X" , "9" , "8" , "7" , "6" , "5" , "4" , "3" , "2" }; int [] wi = new int []{ 7 , 9 , 10 , 5 , 8 , 4 , 2 , 1 , 6 , 3 , 7 , 9 , 10 , 5 , 8 , 4 , 2 , 1 }; int sum = 0 ; for ( int i = 0 ; i < 17 ; i++) { String ch = temp.substring(i, i + 1 ); sum = sum + UtilsString.StringToInteger(ch) * wi[i]; } int y = sum % 11 ; return checkTable[y]; } /** * 身份证号码中是否存在非法字符。 * * @return true: 正确 false:存在非法字符。 */ private boolean isCharCorrect() { boolean iRet = true ; if ( this .isLenCorrect()) { byte [] temp = this .idCardNum.getBytes(); if ( this .is15()) { for ( int i = 0 ; i < temp.length; i++) { if (temp[i] < 48 || temp[i] > 57 ) { iRet = false ; break ; } } } if ( this .is18()) { for ( int i = 0 ; i < temp.length; i++) { if (temp[i] < 48 || temp[i] > 57 ) { if (i == 17 && temp[i] != 88 ) { iRet = false ; break ; } } } } } else { iRet = false ; } return iRet; } /** * 判断身份证的出生年份是否未闰年。 * * @return true :闰年 false 平年 */ private boolean isLeapyear() { String temp; if ( this .is15()) { temp = "19" + this .idCardNum.substring( 6 , 8 ); } else { temp = this .idCardNum.substring( 6 , 10 ); } int year = UtilsString.StringToInteger(temp); if ((year % 4 == 0 && year % 100 != 0 ) || year % 400 == 0 ) return true ; else return false ; } } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 我与微信审核的“相爱相杀”看个人小程序副业
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~
2021-05-27 MMKV使用笔记