肿瘤康复网,内容丰富有趣,生活中的好帮手!
肿瘤康复网 > Java判断身份证号码是否正确

Java判断身份证号码是否正确

时间:2019-04-08 22:28:08

相关推荐

方法如下:

public static final int IDENTITYCODE_OLD = 15; // 老身份证15位public static final int IDENTITYCODE_NEW = 18; // 新身份证18位public static int[] Wi = new int[17];/*** 判断身份证号码是否正确。* * @param code* 身份证号码。* @return 如果身份证号码正确,则返回true,否则返回false。*/public static boolean isIdentityCode(String code) {if (StringUtils.isEmpty(code)) {return false;}String birthDay = "";code = code.trim().toUpperCase();// 长度只有15和18两种情况if ((code.length() != IDENTITYCODE_OLD)&& (code.length() != IDENTITYCODE_NEW)) {return false;}// 身份证号码必须为数字(18位的新身份证最后一位可以是x)Pattern pt = pile("(^\\d{15}$)|(\\d{17}(?:\\d|x|X)$)");Matcher mt = pt.matcher(code);if (!mt.find()) {return false;}// 验证生日if (code.length() == IDENTITYCODE_OLD) {birthDay = "19" + code.substring(6, 12);} else {birthDay = code.substring(6, 14);}if (DateUtils.dateFormatToDate(birthDay, "yyyyMMdd") == null) {return false;}// 最后一位校验码验证if (code.length() == IDENTITYCODE_NEW) {String lastNum = getCheckFlag(code.substring(0,IDENTITYCODE_NEW - 1));// check last digitif (!("" + code.charAt(IDENTITYCODE_NEW - 1)).toUpperCase().equals(lastNum)) {return false;}}return true;}

如果觉得《Java判断身份证号码是否正确》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。