身份证号验证 以及 判断时间格式 格式必须为“YYYY-MM-dd”

    final static Map<Integer, String> zoneNum = new HashMap<Integer, String>();

    static {
        zoneNum.put( 11, "北京" );
        zoneNum.put( 12, "天津" );
        zoneNum.put( 13, "河北" );
        zoneNum.put( 14, "山西" );
        zoneNum.put( 15, "内蒙古" );
        zoneNum.put( 21, "辽宁" );
        zoneNum.put( 22, "吉林" );
        zoneNum.put( 23, "黑龙江" );
        zoneNum.put( 31, "上海" );
        zoneNum.put( 32, "江苏" );
        zoneNum.put( 33, "浙江" );
        zoneNum.put( 34, "安徽" );
        zoneNum.put( 35, "福建" );
        zoneNum.put( 36, "江西" );
        zoneNum.put( 37, "山东" );
        zoneNum.put( 41, "河南" );
        zoneNum.put( 42, "湖北" );
        zoneNum.put( 43, "湖南" );
        zoneNum.put( 44, "广东" );
        zoneNum.put( 45, "广西" );
        zoneNum.put( 46, "海南" );
        zoneNum.put( 50, "重庆" );
        zoneNum.put( 51, "四川" );
        zoneNum.put( 52, "贵州" );
        zoneNum.put( 53, "云南" );
        zoneNum.put( 54, "西藏" );
        zoneNum.put( 61, "陕西" );
        zoneNum.put( 62, "甘肃" );
        zoneNum.put( 63, "青海" );
        zoneNum.put( 64, "宁夏" );
        zoneNum.put( 65, "新疆" );
        zoneNum.put( 71, "台湾" );
        zoneNum.put( 81, "香港" );
        zoneNum.put( 82, "澳门" );
        zoneNum.put( 91, "外国" );
    }

    final static int[] PARITYBIT = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
    final static int[] POWER_LIST = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};

    /**
     * 身份证验证
     *
     * @param certNo 号码内容
     * @return 是否有效 null和"" 都是false
     */
    public static boolean isIDCard(String certNo) {
        if (certNo == null || (certNo.length() != 15 && certNo.length() != 18))
            return false;
        final char[] cs = certNo.toUpperCase().toCharArray();
        //校验位数
        int power = 0;
        for (int i = 0; i < cs.length; i++) {
            if (i == cs.length - 1 && cs[i] == 'X')
                break;//最后一位可以 是X或x
            if (cs[i] < '0' || cs[i] > '9')
                return false;
            if (i < cs.length - 1) {
                power += (cs[i] - '0') * POWER_LIST[i];
            }
        }

        //校验区位码
        if (!zoneNum.containsKey( Integer.valueOf( certNo.substring( 0, 2 ) ) )) {
            return false;
        }

        //校验年份
        String year = null;
        year = certNo.length() == 15 ? getIdcardCalendar( certNo ) : certNo.substring( 6, 10 );


        final int iyear = Integer.parseInt( year );
        if (iyear < 1900 || iyear > Calendar.getInstance().get( Calendar.YEAR ))
            return false;//1900年的PASS,超过今年的PASS

        //校验月份
        String month = certNo.length() == 15 ? certNo.substring( 8, 10 ) : certNo.substring( 10, 12 );
        final int imonth = Integer.parseInt( month );
        if (imonth < 1 || imonth > 12) {
            return false;
        }

        //校验天数
        String day = certNo.length() == 15 ? certNo.substring( 10, 12 ) : certNo.substring( 12, 14 );
        final int iday = Integer.parseInt( day );
        if (iday < 1 || iday > 31)
            return false;

        //校验"校验码"
        if (certNo.length() == 15)
            return true;
        return cs[cs.length - 1] == PARITYBIT[power % 11];
    }

    private static String getIdcardCalendar(String certNo) {
        // 获取出生年月日
        String birthday = certNo.substring( 6, 12 );
        SimpleDateFormat ft = new SimpleDateFormat( "yyMMdd" );
        Date birthdate = null;
        try {
            birthdate = ft.parse( birthday );
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }
        Calendar cday = Calendar.getInstance();
        cday.setTime( birthdate );
        String year = String.valueOf( cday.get( Calendar.YEAR ) );
        return year;
    }



 /**
     * 判断时间格式 格式必须为“YYYY-MM-dd”
     * 2004-2-30 是无效的
     * 2003-2-29 是无效的
     *
     * @param birthday
     * @return
     */
    public static boolean isLegalDate(String birthday) {
        int legalLen = 10;
        if ((birthday == null) || (birthday.length() != legalLen)) {
            return false;
        }
        DateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd" );
        try {
            Date date = formatter.parse( birthday );
            return birthday.equals( formatter.format( date ) );
        } catch (Exception e) {
            return false;
        }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是Java判断是否为身份证号码的示例代码: ```java import java.text.ParseException; import java.text.SimpleDateFormat; public class IDCardValidator { public static void main(String[] args) { String idCard = "身份证号码"; // 替换为要验证身份证号码 if (isValidIDCard(idCard)) { String birthday = getBirthday(idCard); System.out.println("生日:" + birthday); } else { System.out.println("身份证号码不合法"); } } // 判断身份证号码是否合法 public static boolean isValidIDCard(String idCard) { // 判断长度是否为18位 if (idCard.length() != 18) { return false; } // 判断前17位是否为数字 String idCard17 = idCard.substring(0, 17); if (!isNumeric(idCard17)) { return false; } // 判断最后一位是否为数字或小写字母 char lastChar = idCard.charAt(17); if (!Character.isDigit(lastChar) && !Character.isLowerCase(lastChar)) { return false; } // 判断日期是否存在 String birthday = idCard.substring(6, 14); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd"); dateFormat.setLenient(false); try { dateFormat.parse(birthday); } catch (ParseException e) { return false; } // 判断最后一位校验码是否正确 if (!isValidCheckCode(idCard)) { return false; } return true; } // 获取身份证号码中的生日 public static String getBirthday(String idCard) { String birthday = idCard.substring(6, 14); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd"); try { SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd"); return outputFormat.format(dateFormat.parse(birthday)); } catch (ParseException e) { return "0000-00-00"; } } // 判断字符串是否为纯数字 public static boolean isNumeric(String str) { for (int i = 0; i < str.length(); i++) { if (!Character.isDigit(str.charAt(i))) { return false; } } return true; } // 判断身份证号码的最后一位校验码是否正确 public static boolean isValidCheckCode(String idCard) { int[] weights = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 }; char[] checkCodes = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' }; int sum = 0; for (int i = 0; i < 17; i++) { sum += (idCard.charAt(i) - '0') * weights[i]; } int index = sum % 11; char checkCode = checkCodes[index]; return idCard.charAt(17) == checkCode; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南大白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值