方法如下:
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 = Pattern.compile("(^\\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;}