驗證Android使用者輸入日期

來源:互聯網
上載者:User

標籤:

如何驗證使用者輸入的日期是有效還是無效?

private Pattern pattern;private Matcher matcher;private static final String DATE_PATTERN =        "(0?[1-9]|1[012]) [/.-] (0?[1-9]|[12][0-9]|3[01]) [/.-] ((19|20)\\d\\d)";/*** Validate date format with regular expression* @param date date address for validation* @return true valid date format, false invalid date format*/public boolean validate(final String date){  matcher = pattern.matcher(date);  if(matcher.matches()){      matcher.reset();      if(matcher.find()){          String day = matcher.group(1);          String month = matcher.group(2);          int year = Integer.parseInt(matcher.group(3));          if (day.equals("31") &&            (month.equals("4") || month .equals("6") || month.equals("9") ||                   month.equals("11") || month.equals("04") || month .equals("06") ||                   month.equals("09"))) {             return false; // only 1,3,5,7,8,10,12 has 31 days          }                   }      else{           return false;      }          }  else{      return false;  }              }
private Pattern pattern;private Matcher matcher;private static final String DATE_PATTERN =        "(0?[1-9]|1[012]) [/.-] (0?[1-9]|[12][0-9]|3[01]) [/.-] ((19|20)\\d\\d)";  /**   * Validate date format with regular expression   * @param date date address for validation   * @return true valid date format, false invalid date format   */public boolean validate(final String date){  matcher = pattern.matcher(date);  if(matcher.matches()){      matcher.reset();      if(matcher.find()){          String day = matcher.group(1);          String month = matcher.group(2);          int year = Integer.parseInt(matcher.group(3));          if (day.equals("31") &&             (month.equals("4") || month .equals("6") || month.equals("9") ||                   month.equals("11") || month.equals("04") || month .equals("06") ||                   month.equals("09"))) {             return false; // only 1,3,5,7,8,10,12 has 31 days          }           else if (month.equals("2") || month.equals("02")) {               //leap year               if(year % 4==0){                   if(day.equals("30") || day.equals("31")){                       return false;                   }                   else{                       return true;                   }              }              else{                  if(day.equals("29")||day.equals("30")||day.equals("31")){                      return false;                  }                  else{                      return true;                  }              }          }          else{                             return true;                          }      }      else{           return false;      }          }  else{      return false;  }              }

再將下面的代碼放到onClick()中:

matcher = Pattern.compile(DATE_PATTERN).matcher(Birthday);//Birthday validator    else if (!matcher.matches()) {    Toast.makeText(getApplicationContext(), "Invalid Birthday!", Toast.LENGTH_SHORt).show();                    }

 

驗證Android使用者輸入日期

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.