美文网首页Android开发
JAVA校验身份证号码

JAVA校验身份证号码

作者: 水门 | 来源:发表于2018-04-18 14:54 被阅读39次

    以下为java校验身份证号码的代码:

    import java.util.Calendar;
    import java.util.Scanner;
    
    public class Verify {
    
        public static void main(String[] args) {
            System.out.println("请输入身份证号:");
            Scanner scanner = new Scanner(System.in);
            System.out.println(isIDCardNo(scanner.next()));
        }
    
        public static boolean isIDCardNo(String id) {
            if (id == null)
                return false;
            id = id.toUpperCase();
            if (id.length() != 15 && id.length() != 18) {
                return false;
            }
            int y = 0, m = 0, d = 0;
            if (id.length() == 15) {
                y = Integer.parseInt("19" + id.substring(6, 8), 10);
                m = Integer.parseInt(id.substring(8, 10), 10);
                d = Integer.parseInt(id.substring(10, 12), 10);
            } else if (id.length() == 18) {
                if (id.indexOf("X") >= 0 && id.indexOf("X") != 17) {
                    return false;
                }
                char verifyBit = 0;
                int sum = (id.charAt(0) - '0') * 7 + (id.charAt(1) - '0') * 9 + (id.charAt(2) - '0') * 10
                        + (id.charAt(3) - '0') * 5 + (id.charAt(4) - '0') * 8 + (id.charAt(5) - '0') * 4
                        + (id.charAt(6) - '0') * 2 + (id.charAt(7) - '0') * 1 + (id.charAt(8) - '0') * 6
                        + (id.charAt(9) - '0') * 3 + (id.charAt(10) - '0') * 7 + (id.charAt(11) - '0') * 9
                        + (id.charAt(12) - '0') * 10 + (id.charAt(13) - '0') * 5 + (id.charAt(14) - '0') * 8
                        + (id.charAt(15) - '0') * 4 + (id.charAt(16) - '0') * 2;
                sum = sum % 11;
                switch (sum) {
                    case 0:
                        verifyBit = '1';
                        break;
                    case 1:
                        verifyBit = '0';
                        break;
                    case 2:
                        verifyBit = 'X';
                        break;
                    case 3:
                        verifyBit = '9';
                        break;
                    case 4:
                        verifyBit = '8';
                        break;
                    case 5:
                        verifyBit = '7';
                        break;
                    case 6:
                        verifyBit = '6';
                        break;
                    case 7:
                        verifyBit = '5';
                        break;
                    case 8:
                        verifyBit = '4';
                        break;
                    case 9:
                        verifyBit = '3';
                        break;
                    case 10:
                        verifyBit = '2';
                        break;
    
                }
    
                if (id.charAt(17) != verifyBit) {
                    return false;
                }
                y = Integer.parseInt(id.substring(6, 10), 10);
                m = Integer.parseInt(id.substring(10, 12), 10);
                d = Integer.parseInt(id.substring(12, 14), 10);
            }
    
            int currentY = Calendar.getInstance().get(Calendar.YEAR);
    
            /*
             * if(isGecko){ currentY += 1900; }
             */
            if (y > currentY || y < 1870) {
                return false;
            }
            if (m < 1 || m > 12) {
                return false;
            }
            if (d < 1 || d > 31) {
                return false;
            }
            return true;
        }
    
    }
    

    相关文章

      网友评论

        本文标题:JAVA校验身份证号码

        本文链接:https://www.haomeiwen.com/subject/abivkftx.html