Validate date in edittext box

2020-03-31 06:06发布

问题:

I am using the following code to validate date entered in edittextbox.however in xml file I had given its input type date.

int i = validate(registerdate);

this is validate function:

private int  validate(String registerdate) {

        String regEx =
            "^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$";
          Matcher matcherObj = Pattern.compile(regEx).matcher(registerdate);
          if (matcherObj.matches())
            {
                return 1;
            }
            else
            {
                return 0;
            }
    }

its giving me compile time error at

String regEx =
            "^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$";

saying invalid escape sequences.

Can any one figure out where I am wrong or any other way to validate date expression?

回答1:

Java will treat \ inside a string as starting an escape sequence. Make sure you use \ instead (so that you get an actual \ character in the string) and you should be ok.

Quick Update: As Etienne points out, if you actually want a \ in the RegEx itself, you'll need to use \\, since that will produce \ in the string, which will produce \ in the RegEx.

Your regex after correction:

String regEx ="^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\\d{2}$";


回答2:

I hope this will help :)

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;
     }              
   }

and put these codes on your onClick():

matcher = Pattern.compile(DATE_PATTERN).matcher(Birthday);

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