随机输出任意个数合法身份证号码

it2022-05-05  116

应用前建议输入的数字小一点。如果输入个数太大,计算机配置不高会很慢

import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Random; import java.util.Scanner; public class ID_Card { public static void main(String[] args) { System.out.print("请输入需要的合法身份证的个数:"); Scanner sc = new Scanner(System.in); int num = sc.nextInt(); List<String> list= randomArray(num) ; for (String string : list) { if (test_Id_Care(String.valueOf(string)) ) { System.out.println(String.valueOf(string)+"合法"); } } } public static List<String> randomArray(int num) { List<String> list = new ArrayList<>(); while(true){ String[] a =new String[18]; Random random = new Random(); for (int i = 0; i < a.length; i++) { a[i] =String.valueOf(random.nextInt(10)); } String str=""; for (String string : a) { str+=string; } if (test_Id_Care(String.valueOf(str)) ) { list.add(str); } if (list.size()==num) { break; } } return list; } /** * 验证身份证号码 * * @param id_number * @return */ public static boolean test_Id_Care(String id_number) { if (checkCardIdLastNum(id_number) == true) {// 验证身份证长度以及第十八位校验位(要求18位身份证) if (checkProvinceId(id_number) == true) {// 检查身份证的省份信息是否正确(使用与18/15位身份证) if (isValidDate(id_number.substring(6, 14)) == true) {// 判断日期是否有效 return true; } else { return false; } } else { return false; } } else { return false; } } // 身份证前1位每位加权因子 private static int[] power = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 }; // 身份证第18位校检码 private static String[] refNumber = { "1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2" }; // 省(直辖市)代码表 private static String provinceCode[] = { "11", "12", "13", "14", "15", "21", "22", "23", "31", "32", "33", "34", "35", "36", "37", "41", "42", "43", "44", "45", "46", "50", "51", "52", "53", "54", "61", "62", "63", "64", "65", "71", "81", "82"}; /** * 检查身份证的省份信息是否正确(使用与18/15位身份证) * * @param provinceid * @return */ public static boolean checkProvinceId(String provinceid) { for (String id : provinceCode) { if (id.equals(provinceid.substring(0, 2))) { return true; } } return false; } /** * 校验身份证第18位是否正确(只适合18位身份证) * * @param cardId * @return */ public static boolean checkCardIdLastNum(String cardId) { if (cardId.length() != 18) { return false; } char[] tmp = cardId.toCharArray(); int[] cardidArray = new int[tmp.length - 1]; int i = 0; for (i = 0; i < tmp.length - 1; i++) { cardidArray[i] = Integer.parseInt(tmp[i] + ""); } try { String checkCode = sumPower(cardidArray); String lastNum = tmp[tmp.length - 1] + ""; if (lastNum.equals("x")) { lastNum = lastNum.toUpperCase(); } if (!checkCode.equals(lastNum)) { return false; } return true; } catch (Exception e) { return false; } } /** * 计算身份证的第十八位校验码 * * @param cardidArray * @return */ public static String sumPower(int[] cardidArray) { int result = 0; for (int i = 0; i < power.length; i++) { result += power[i] * cardidArray[i]; } return refNumber[(result % 11)]; } /** * 判断日期是否有效 * * @param inDate * @return */ public static boolean isValidDate(String inDate) { if (inDate == null) { return false; } Date date = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd"); if (inDate.trim().length() != dateFormat.toPattern().length()) { return false; } try { if (dateFormat.parse(inDate).getTime() - date.getTime() > 0 || dateFormat.parse(inDate).getTime() < -2209017600000L) { return false; } } catch (ParseException e1) { // TODO Auto-generated catch block return false; } dateFormat.setLenient(false);// 严格的日期匹配 try { dateFormat.parse(inDate.trim()); } catch (ParseException e) { return false; } return true; } }

最新回复(0)