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

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

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

    鷹翔宇空

    學(xué)習(xí)和生活

    BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
      110 Posts :: 141 Stories :: 315 Comments :: 1 Trackbacks

    這兩天做一個(gè)身份證校驗(yàn)的問題,碰到了日期校驗(yàn)的問題,為了搞的簡單一點(diǎn),查了很多資料,但都不太理想。后來想到一個(gè)方法,那就是通過值比較的方法。比如你要校驗(yàn)的日期為:2005-12-28,你可以將它從String類型轉(zhuǎn)換為java.sql.Date類型。轉(zhuǎn)換后,再將它toString(),,這樣就會(huì)得到一個(gè)新的字符串。然后比較這兩個(gè)字符串是否相等,不等就意味著這個(gè)日期不是合法的日期。因?yàn)椋谡{(diào)用java.sql.Date.ValueOf()方法時(shí),只要日期格式正確的,當(dāng)然了,還必須是數(shù)字,那它就會(huì)自動(dòng)為你轉(zhuǎn)換為合法的日期,它會(huì)自動(dòng)幫你做日期的加減運(yùn)算,所以你是沒法通過這些來校驗(yàn)的,這樣轉(zhuǎn)換過后呢,如果日期本來就不合法,例如:2005-15-12

    它就會(huì)自動(dòng)轉(zhuǎn)換為:2006-03-12,也就是說兩個(gè)新的字符串就發(fā)生了改變,這樣就可以比較來很快的確認(rèn)日期是否合法了。當(dāng)然如果,要校驗(yàn)的日期不是數(shù)字,那么,在進(jìn)行java.sql.Date.ValueOf()時(shí)就會(huì)拋異常的,但是如果格式不正確,還可以通過java.text.SimpleDateFormat函數(shù)來轉(zhuǎn)換,如獲取當(dāng)前的系統(tǒng)日期,可以這樣:

      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;

      }

    下面是一個(gè)身份證校驗(yàn)的方法(說明:除了日期校驗(yàn)和數(shù)字校驗(yàn)外,其他校驗(yàn)方法是從互聯(lián)網(wǎng)上搜索得到的,但卻沒有搜到原作者,也沒有搜到原出處,以后一定補(bǔ)上,特此致歉。)

    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;//將日期轉(zhuǎn)換為字符串(中間無符號(hào))

        String verifyCode = null;       //最后一位校驗(yàn)位

        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;

      }

    //獲取當(dāng)前系統(tǒng)日期

      /**

       * 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 閱讀(2645) 評(píng)論(3)  編輯  收藏 所屬分類: 學(xué)習(xí)體會(huì)

    Feedback

    # re: 一個(gè)日期校驗(yàn)方法介紹和一個(gè)身份證的校驗(yàn)方法 2005-12-28 18:53 漢尼
    用正則表達(dá)式不是很好嗎??  回復(fù)  更多評(píng)論
      

    # re: 一個(gè)日期校驗(yàn)方法介紹和一個(gè)身份證的校驗(yàn)方法 2005-12-29 08:39 TrampEagle
    TO: 漢尼
    謝謝,如果能用正則表達(dá)式校驗(yàn)日期當(dāng)然更好,不過,我也是剛接觸正則表達(dá)式時(shí)間不是太長,所以只是用于一些簡單的校驗(yàn),但我會(huì)繼續(xù)學(xué)習(xí),會(huì)繼續(xù)優(yōu)化它的。  回復(fù)  更多評(píng)論
      

    # re: 一個(gè)日期校驗(yàn)方法介紹和一個(gè)身份證的校驗(yàn)方法 2006-03-23 10:22 TrampEagle
    日期校驗(yàn)的一個(gè)小方法(用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;
    }
    其實(shí)其他語言也可以的,方法也都一樣的,很老套,哈哈哈
      回復(fù)  更多評(píng)論
      

    主站蜘蛛池模板: 亚洲国产成人精品无码区二本| 亚洲人成网站在线观看播放| 亚洲人成影院在线高清| 日本免费大黄在线观看| 午夜影视日本亚洲欧洲精品一区 | 亚洲va无码va在线va天堂| 免费精品视频在线| 亚洲国产精品碰碰| 色噜噜狠狠色综合免费视频| 四虎影视精品永久免费| 人体大胆做受免费视频| 国产午夜亚洲不卡| a级片在线免费看| 久久夜色精品国产噜噜亚洲AV| 曰批全过程免费视频播放网站| 亚洲网站免费观看| 国产成人免费高清激情视频| 韩国亚洲伊人久久综合影院| 免费a级毛片在线观看| 三年片免费高清版 | 亚洲va精品中文字幕| 成人免费淫片在线费观看| 小说区亚洲自拍另类| 伊人久久综在合线亚洲91| 三年片在线观看免费西瓜视频| 亚洲伦理一区二区| 免费看成人AA片无码视频羞羞网| 亚洲av成人一区二区三区在线播放 | 亚洲AV无码久久精品色欲| 97人妻无码一区二区精品免费| 亚洲AV无码专区在线电影成人| 亚洲国产成人精品女人久久久 | 免费A级毛片无码A∨免费| 亚洲一卡2卡三卡4卡无卡下载| 亚洲乱码中文字幕手机在线| 无码国产精品一区二区免费3p| 亚洲精品中文字幕无码A片老| 亚洲人成人一区二区三区| 国产高清免费视频| 九九免费久久这里有精品23| 亚洲天堂一区二区三区|