Correct java date validation check?

There is a edittext to enter the date from the datepicker in the format dd. MM. yyyy

//считываю выбранный день месяц год
int day = datePicker.getDayOfMonth();
int mon = datePicker.getMonth();
int year = datePicker.getYear();
//в переменную записываю дату строкой
String outdate = String.format("%02d", day) + "." + String.format("%02d", mon + 1) + "." + year;
DateEnterText.setText(outdate); //передаю значение в поле

I also want to do manual input in edittext, but how would it be more correct to check the validity (correctness) of the entered date, first of all that it corresponds to the format dd. MM. yyyy and the fact that for example the entered date will not be some February 29 not in a leap year or for example 35.03.2020. How to do this correctly do it?

When entering a value manually in edittext, I already automatically put dividing "points" through the TextWatcher class, and there is a limit on the number of characters entered android:maxLength="10", android:inputType="date", also in other words, the input order seems to be made.

Thanks. I'm just learning...don't kick too hard and this is my first question here...

Author: Эникейщик, 2020-08-15

1 answers

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

This code is based on the onClick method()

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

//Birthday validator
    else if (!matcher.matches()) {
    Toast.makeText(getApplicationContext(), "Invalid Birthday!", Toast.LENGTH_SHORt).show();                    
}
 0
Author: Мирослав Стрельбицкий, 2020-08-17 06:24:26