枚举身份证后四位java代码
class Test {
public static void main(String[] args) {
char[] arr = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'X'};
ArrayList<String> list = new ArrayList<>();
char[] id = "xxxxxxxxxxxxxx0000".toCharArray();
for (int i = 0; i < arr.length - 1; i++) {
id[14] = arr[i];
for (int i1 = 0; i1 < arr.length - 1; i1++) {
id[15] = arr[i1];
for (int i2 = 0; i2 < arr.length - 1; i2++) {
if (Integer.parseInt(arr[i2]+"") % 2 == 0) {
id[16] = arr[i2];
for (int i3 = 0; i3 < arr.length; i3++) {
id[17] = arr[i3];
String temp = String.valueOf(id);
if (IdentificationCodeUtil.isIdentityCode(temp)) {
System.out.println(temp);
list.add(temp);
}
}
}
}
}
}
System.out.println(list.size());
System.out.println(list.get(0));
}
static class IdentificationCodeUtil {
public static final int IDENTITYCODE_OLD = 15;
public static final int IDENTITYCODE_NEW = 18;
public static int[] Wi = new int[17];
public static boolean isIdentityCode(String code) {
if (code == null || "".equals(code.trim())) {
return false;
}
String birthDay = "";
code = code.trim();
if ((code.length() != IDENTITYCODE_OLD)
&& (code.length() != IDENTITYCODE_NEW)) {
return false;
}
Pattern pt = Pattern.compile("\\d{15,17}([\\dxX]{1})?");
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);
}
try {
new SimpleDateFormat("yyyyMMdd").parse(birthDay);
} catch (ParseException e) {
return false;
}
if (code.length() == IDENTITYCODE_NEW) {
String lastNum = getCheckFlag(code.substring(0,
IDENTITYCODE_NEW - 1));
if (!("" + code.charAt(IDENTITYCODE_NEW - 1)).toUpperCase().equals(
lastNum)) {
return false;
}
}
return true;
}
private static String getCheckFlag(String code) {
int[] varArray = new int[code.length()];
String lastNum = "";
int numSum = 0;
setWiBuffer();
for (int i = 0; i < code.length(); i++) {
varArray[i] = new Integer("" + code.charAt(i)).intValue();
varArray[i] = varArray[i] * Wi[i];
numSum = numSum + varArray[i];
}
int checkDigit = 12 - numSum % 11;
switch (checkDigit) {
case 10:
lastNum = "X";
break;
case 11:
lastNum = "0";
break;
case 12:
lastNum = "1";
break;
default:
lastNum = String.valueOf(checkDigit);
}
return lastNum;
}
private static void setWiBuffer() {
for (int i = 0; i < Wi.length; i++) {
int k = (int) Math.pow(2, (Wi.length - i));
Wi[i] = k % 11;
}
}
public static boolean empty(String o) {
return ((null == o) || (o.length() <= 0) || (o.trim().equals("")));
}
public static String update2eighteen(String code) {
if (code == null || "".equals(code.trim())) {
return "";
}
code = code.trim();
if (code.length() != IDENTITYCODE_OLD || !isIdentityCode(code)) {
return "";
}
code = code.substring(0, 6) + "19" + code.substring(6);
code = code + getCheckFlag(code);
return code;
}
public static String resume2fifteen(String code){
if (code == null || "".equals(code.trim())) {
return "";
}
code = code.trim();
if (code.length() != IDENTITYCODE_NEW ) {
return "";
}
StringBuffer codebuffer = new StringBuffer(code);
codebuffer.delete(6, 8);
codebuffer.deleteCharAt(codebuffer.length() -1 );
return codebuffer.toString();
}
public static void main(String[] args){
String[] codeArray = new String[]{"62272219850510261x","53010119810602007x","53010119810602001x"};
for(int i= 0;i<codeArray.length;i++){
if(isIdentityCode(codeArray[i])){
System.out.println(codeArray[i]+":为正确的身份证号码!");
}else{
System.out.println(codeArray[i]+":为错误的身份证号码!");
}
}
System.out.println("转换后的身份证号码为:"+update2eighteen("330521820721052"));;
System.out.println("转换后的身份证号码为:"+resume2fifteen("62272219850510261x"));;
}
}}