• 验证Android用户输入日期


    如何验证用户输入的日期是有效还是无效?

    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();                    
    }
  • 相关阅读:
    debug和console.write()有什么区别
    数据源绑定DataGridViewComboBox
    关于SqlDataAdapter的Update()方法
    反思。。
    C语言光标移动
    关于湖南工业大学“蓝桥杯”预选赛
    Left digit
    突然想写个超级马里奥
    如何知道一个数有多大位数
    Hut 新生训练赛第二场 迟来的解题报告
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/5748386.html
Copyright © 2020-2023  润新知