鹰翔宇空

学习和生活

BlogJava 首页 新随笔 联系 聚合 管理
  110 Posts :: 141 Stories :: 315 Comments :: 1 Trackbacks

这两天做一个身份证校验的问题,碰到了日期校验的问题,为了搞的简单一点,查了很多资料,但都不太理想。后来想到一个方法,那就是通过值比较的方法。比如你要校验的日期为:2005-12-28,你可以将它从String类型转换为java.sql.Date类型。转换后,再将它toString(),,这样就会得到一个新的字符串。然后比较这两个字符串是否相等,不等就意味着这个日期不是合法的日期。因为,在调用java.sql.Date.ValueOf()方法时,只要日期格式正确的,当然了,还必须是数字,那它就会自动为你转换为合法的日期,它会自动帮你做日期的加减运算,所以你是没法通过这些来校验的,这样转换过后呢,如果日期本来就不合法,例如:2005-15-12

它就会自动转换为:2006-03-12,也就是说两个新的字符串就发生了改变,这样就可以比较来很快的确认日期是否合法了。当然如果,要校验的日期不是数字,那么,在进行java.sql.Date.ValueOf()时就会抛异常的,但是如果格式不正确,还可以通过java.text.SimpleDateFormat函数来转换,如获取当前的系统日期,可以这样:

  public static java.sql.Date getSystemDate() throws Exception {

    SimpleDateFormat tempSimpleDateFormat = null;

    Date currentDate = null;

    try{

      tempSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

      currentDate = java.sql.Date.valueOf(tempSimpleDateFormat.format(new java.

          util.Date()));

    }catch(Exception ex){

      ex.printStackTrace();

      throw ex;

    }finally{

      tempSimpleDateFormat = null;

    }

    return currentDate;

  }

下面是一个身份证校验的方法(说明:除了日期校验和数字校验外,其他校验方法是从互联网上搜索得到的,但却没有搜到原作者,也没有搜到原出处,以后一定补上,特此致歉。)

package com.hyq.test.src;

 

import java.text.SimpleDateFormat;

import java.sql.Date;

import java.util.regex.Pattern;

import java.util.regex.Matcher;

 

public class VerifyIdcard {

  public VerifyIdcard() {

  }

  /**

   * VerifyIdCard

   *

   * @param idcard String

   * @return boolean

   */

  public boolean VerifyIdCard(String idcard) {

    Pattern pattern = null;

    Matcher matcher = null;

    Date currentDate = null;

    Date conversionDateOne = null;  //从身份证获取日期

    String year = null;             //从身份证获取年

    String month = null;            //从身份证获取月

    String day = null;              //从身份证获取日

    String conversionDateTwo = null;//将日期转换为字符串(中间无符号)

    String verifyCode = null;       //最后一位校验位

    if (idcard.length() == 15) {

      idcard = uptoeighteen(idcard);

    }

    if (idcard.length() != 18) {

      return false;

    }

    try{

      pattern = Pattern.compile("\\d{17}");

      matcher = pattern.matcher(idcard);

      if (!matcher.find()) {

        return false;

      }

      year = idcard.substring(6, 10);

      month = idcard.substring(10, 12);

      day = idcard.substring(12, 14);

      long dateOne = Long.parseLong(idcard.substring(6, 14));

      currentDate = this.getSystemDate();

      conversionDateOne = java.sql.Date.valueOf(year + "-" + month + "-" +

                                                day);

      if (currentDate.compareTo(conversionDateOne) <= 0) {

        return false;

      }

      conversionDateTwo = conversionDateOne.toString().substring(0, 4) +

          conversionDateOne.toString().substring(5, 7) +

          conversionDateOne.toString().substring(8);

      long dateTwo = Long.parseLong(conversionDateTwo);

      if (dateTwo > dateOne) {

        return false;

      }

      verifyCode = idcard.substring(17);

      if(verifyCode != null && verifyCode.equals(this.getVerify(idcard.substring(0,17)))){

        return true;

      }else{

        return false;

      }

    }catch(Exception ex){

      return false;

    }finally{

      pattern = null;

      matcher = null;

      currentDate = null;

      conversionDateOne = null;

      year = null;

      month = null;

      day = null;

      conversionDateTwo = null;

    }

  }

 

//get verify

  /**

   * getVerify

   *

   * @param eightcardid String

   * @return String

   */

  public String getVerify(String eightcardid) {

    int remaining = 0;

    int[] wi = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1};

    int[] vi = {1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2};

    int[] ai = new int[18];

    String returnStr = null;

    try{

      if (eightcardid.length() == 18) {

        eightcardid = eightcardid.substring(0, 17);

      }

      if (eightcardid.length() == 17) {

        int sum = 0;

        String k = null;

        for (int i = 0; i < 17; i++) {

          k = eightcardid.substring(i, i + 1);

          ai[i] = Integer.parseInt(k);

          k = null;

        }

        for (int i = 0; i < 17; i++) {

          sum = sum + wi[i] * ai[i];

        }

        remaining = sum % 11;

      }

      returnStr = remaining == 2 ? "X" : String.valueOf(vi[remaining]);

    }

    catch(Exception ex){

      return null;

    }finally{

      wi = null;

      vi = null;

      ai = null;

    }

    return returnStr;

  }

//获取当前系统日期

  /**

   * getSystemDate

   *

   * @return Date

   */

  public static java.sql.Date getSystemDate(){

    SimpleDateFormat tempSimpleDateFormat = null;

    Date currentDate = null;

    try{

      tempSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

      currentDate = java.sql.Date.valueOf(tempSimpleDateFormat.format(new java.

          util.Date()));

    }catch(Exception ex){

      return null;

    }finally{

      tempSimpleDateFormat = null;

    }

    return currentDate;

  }

 

//15 update to 18

  /**

   * uptoeighteen

   *

   * @param fifteencardid String

   * @return String

   */

  public String uptoeighteen(String fifteencardid) {

    String eightcardid = fifteencardid.substring(0, 6);

    eightcardid = eightcardid + "19";

    eightcardid = eightcardid + fifteencardid.substring(6, 15);

    eightcardid = eightcardid + getVerify(eightcardid);

    return eightcardid;

  }

 

 

  public static void main(String[] args){

    String idCard = "410327198107122438";

    VerifyIdcard tempVerifyIdcard = new VerifyIdcard();

    if(tempVerifyIdcard.VerifyIdCard(idCard)){

      System.out.println("+++++++++++++++++++");

    }else{

      System.out.println("*********************");

    }

  }

}

posted on 2005-12-28 09:03 TrampEagle 阅读(2630) 评论(3)  编辑  收藏 所属分类: 学习体会

Feedback

# re: 一个日期校验方法介绍和一个身份证的校验方法 2005-12-28 18:53 汉尼
用正则表达式不是很好吗??  回复  更多评论
  

# re: 一个日期校验方法介绍和一个身份证的校验方法 2005-12-29 08:39 TrampEagle
TO: 汉尼
谢谢,如果能用正则表达式校验日期当然更好,不过,我也是刚接触正则表达式时间不是太长,所以只是用于一些简单的校验,但我会继续学习,会继续优化它的。  回复  更多评论
  

# re: 一个日期校验方法介绍和一个身份证的校验方法 2006-03-23 10:22 TrampEagle
日期校验的一个小方法(用javascript)
function isValidDate(day, month, year) {
if (month < 1 || month > 12) {
return false;
}
if (day < 1 || day > 31) {
return false;
}
if ((month == 4 || month == 6 || month == 9 || month == 11) &&
(day == 31)) {
return false;
}
if (month == 2) {
var leap = (year % 4 == 0 &&
(year % 100 != 0 || year % 400 == 0));
if (day>29 || (day == 29 && !leap)) {
return false;
}
}
return true;
}
其实其他语言也可以的,方法也都一样的,很老套,哈哈哈
  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: