package test.http01;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class IdCard extends Thread {
public void run() {
int i = 1;
while (true) {
rundo(i);
i++;
try {
Thread.sleep(2000);
} catch (Exception e) {
}
}
}
public void rundo(int i) {
System.out.println("请输入一个身份证号码:");
BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
try {
String ww = s.readLine();
if (ww.length() == 15) {
System.out.println("该居民身份证是旧的身份证!");
getInfof(ww);
Convert(ww);
} else if (ww.length() == 18) {
System.out.println("该居民身份证是新的身份证!");
getInfoe(ww);
} else {
System.out.println("该居民身份证号码位数不符!");
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
IdCard idCard = new IdCard();
idCard.start();
}
public static void getInfof(String s) {
String M = new String(s);
int y = Integer.parseInt(M.substring(6, 8));
int m = Integer.parseInt(M.substring(8, 10));
int d = Integer.parseInt(M.substring(10, 12));
if (m < 1 || m > 12 || d < 1 || d > 31
|| ((m == 4 || m == 6 || m == 9 || m == 11) && d > 30)
|| (m == 2 && (((y + 1900) % 4 > 0 && d > 28) || d > 29))) {
System.out.println("該居民身份證出生年月日错误!");
} else {
System.out.println("该居民出生地:" + M.substring(0, 6));
System.out.println("该居民出生时间为:" + y + "年" + m + "月" + d + "日");
}
int sex = Integer.parseInt(M.substring(14, 15));
if (sex % 2 == 0) {
System.out.println("该居民为:女性");
} else {
System.out.println("该居民为:男性");
}
}
public static void getInfoe(String s) {
String M = new String(s);
int y = Integer.parseInt(M.substring(6, 10));
int m = Integer.parseInt(M.substring(10, 12));
int d = Integer.parseInt(M.substring(12, 14));
int[] xx = { 2, 4, 8, 5, 10, 9, 7, 3, 6, 1, 2, 4, 8, 5, 10, 9, 7 };
char[] yy = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };
int mm = 0;
int[] gg = new int[18];
if (y < 1900 || m < 1 || m > 12 || d < 1 || d > 31
|| ((m == 4 || m == 6 || m == 9 || m == 11) && d > 30)
|| (m == 2 && ((y % 4 > 0 && d > 28) || d > 29))) {
System.out.println("該居民身份證出生年月日錯誤!");
} else {
System.out.println("该居民出生地:" + M.substring(0, 6));
System.out.println("该居民出生时间为:" + y + "年" + m + "月" + d + "日");
}
int sex = Integer.parseInt(M.substring(16, 17));
if (sex % 2 == 0) {
System.out.println("该居民为:女性");
} else {
System.out.println("该居民为:男性");
}
for (int i = 1; i < 18; i++) {
int j = 17 - i;
gg[i - 1] = Integer.parseInt(M.substring(j, j + 1));
}
for (int i = 0; i < 17; i++) {
mm += xx[i] * gg[i];
}
mm = mm % 11;
char c = M.charAt(17);
if (c == yy[mm])
System.out.println("该居民身份证是真的");
else
System.out.println("该居民身份证是假的(校验码有误)");
}
public static void Convert(String s) throws IOException {
StringBuffer sad = new StringBuffer(s);
sad.insert(6, "19");
int[] xx = { 2, 4, 8, 5, 10, 9, 7, 3, 6, 1, 2, 4, 8, 5, 10, 9, 7 };
char[] yy = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };
int mm = 0;
int[] gg = new int[18];
for (int i = 1; i < 18; i++) {
int j = 17 - i;
gg[i - 1] = Integer.parseInt(sad.substring(j, j + 1));
}
for (int i = 0; i < 17; i++) {
mm += xx[i] * gg[i];
}
mm = mm % 11;
System.out.println("该居民身份证是新的身份证" + sad + yy[mm]);
}
}