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

3 篇文章 0 订阅

方法如下:

	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 digit
			if (!("" + code.charAt(IDENTITYCODE_NEW - 1)).toUpperCase().equals(
					lastNum)) {
				return false;
			}
		}

		return true;
	}
  • 1
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是Java判断是否身份证号码的示例代码: ```java import java.text.ParseException; import java.text.SimpleDateFormat; public class IDCardValidator { public static void main(String[] args) { String idCard = "身份证号码"; // 替换为要验证的身份证号码 if (isValidIDCard(idCard)) { String birthday = getBirthday(idCard); System.out.println("生日:" + birthday); } else { System.out.println("身份证号码不合法"); } } // 判断身份证号码是否合法 public static boolean isValidIDCard(String idCard) { // 判断长度是否为18位 if (idCard.length() != 18) { return false; } // 判断前17位是否为数字 String idCard17 = idCard.substring(0, 17); if (!isNumeric(idCard17)) { return false; } // 判断最后一位是否为数字或小写字母 char lastChar = idCard.charAt(17); if (!Character.isDigit(lastChar) && !Character.isLowerCase(lastChar)) { return false; } // 判断日期是否存在 String birthday = idCard.substring(6, 14); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd"); dateFormat.setLenient(false); try { dateFormat.parse(birthday); } catch (ParseException e) { return false; } // 判断最后一位校验码是否正确 if (!isValidCheckCode(idCard)) { return false; } return true; } // 获取身份证号码中的生日 public static String getBirthday(String idCard) { String birthday = idCard.substring(6, 14); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd"); try { SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd"); return outputFormat.format(dateFormat.parse(birthday)); } catch (ParseException e) { return "0000-00-00"; } } // 判断字符串是否为纯数字 public static boolean isNumeric(String str) { for (int i = 0; i < str.length(); i++) { if (!Character.isDigit(str.charAt(i))) { return false; } } return true; } // 判断身份证号码的最后一位校验码是否正确 public static boolean isValidCheckCode(String idCard) { int[] weights = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 }; char[] checkCodes = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' }; int sum = 0; for (int i = 0; i < 17; i++) { sum += (idCard.charAt(i) - '0') * weights[i]; } int index = sum % 11; char checkCode = checkCodes[index]; return idCard.charAt(17) == checkCode; } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值