<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    鷹翔宇空

    學習和生活

    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 閱讀(2646) 評論(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;
    }
    其實其他語言也可以的,方法也都一樣的,很老套,哈哈哈
      回復  更多評論
      

    主站蜘蛛池模板: 国产成人亚洲午夜电影| 污视频网站免费在线观看| 精品久久久久久久免费人妻| 久久精品熟女亚洲av麻豆| 亚洲乳大丰满中文字幕| 日韩免费一区二区三区在线| 日亚毛片免费乱码不卡一区| 亚洲午夜久久影院| 伊人久久亚洲综合影院| 亚洲精品免费在线视频| 日韩毛片一区视频免费| 78成人精品电影在线播放日韩精品电影一区亚洲 | 久久亚洲国产成人精品无码区| 永久免费视频网站在线观看| 一个人免费观看视频在线中文| 亚洲人成在线播放网站岛国| 亚洲AV成人精品日韩一区18p| 久草视频免费在线| 99re6在线视频精品免费| 亚洲第一成年网站视频 | 亚洲色成人网站WWW永久四虎| 亚洲熟妇av一区二区三区| 成人a视频片在线观看免费| 精品国产污污免费网站| 视频一区在线免费观看| 亚洲a级片在线观看| 亚洲国语精品自产拍在线观看 | 羞羞漫画在线成人漫画阅读免费| 亚洲综合区图片小说区| 亚洲人成伊人成综合网久久久| 日本免费高清一本视频| 中文字幕无码不卡免费视频| 久久国产精品一区免费下载| 日本免费精品一区二区三区| 亚洲欧美国产精品专区久久| 亚洲乱码一二三四区国产| 久久久久亚洲AV无码专区首| 亚洲乱亚洲乱妇无码麻豆| 亚洲AV无码乱码在线观看| 香蕉视频在线观看免费国产婷婷| 国产卡二卡三卡四卡免费网址|