身份证验证
来源:互联网 发布:mac 移除桌面图标 编辑:程序博客网 时间:2024/04/26 22:17
- import java.util.*;
-
- public class Certificate {
-
- public static final int[] IW = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
- public static final char[] szVerCode = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
-
-
-
-
-
-
- public static boolean valid(String str) {
-
- if (str == null) {
- return false;
- }
-
- str = str.trim();
-
- if (str.length() != 15 && str.length() != 18) {
- return false;
- }
-
- if (str.length() == 15) {
- return true;
- } else {
-
- return str.charAt(17) == getVerifyCode(str);
- }
- }
-
-
-
-
-
-
- public static char getVerifyCode(String str) {
- if (str != null && str.length() >= 17) {
- int IS = 0;
- for (int i = 0; i < 17; i++) {
- IS += (str.charAt(i) - '0') * IW[i];
- }
- return szVerCode[IS % 11];
- } else {
- return '-';
- }
- }
-
-
-
-
-
-
- public static Date getBirthday(String str) {
- if (valid(str)) {
- String birthday = str.length()==15?("19"+str.substring(6,12)):(str.substring(6,14));
- return DateTools.parse(birthday,"yyyyMMdd");
- } else {
- return null;
- }
- }
-
-
-
-
-
-
- public static int getGender(String str) {
- if(valid(str)){
- return (str.length()==15?str.charAt(14):str.charAt(16))%2==1?1:0;
- }else{
- return -1;
- }
- }
- }