How do I verify that the date entered by the user is valid or invalid?
Privatepattern pattern;PrivateMatcher Matcher;Private StaticFinal 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 for MAT, false Invalid date format*/ PublicBoolean 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); intYear = Integer.parseint (Matcher.group (3)); if(Day.equals (" to") &&(Month.equals ("4") || month. Equals ("6") || Month.equals ("9") ||Month.equals (" One") || Month.equals ("Geneva") || month. Equals (" .") ||Month.equals (" the"))) { return false;//Only 1,3,5,7,8,10,12 have } } Else{ return false; } } Else{ return false; } }
Privatepattern pattern;PrivateMatcher Matcher;Private StaticFinal 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 the date date address for validation * @return true Valid Date format, false invalid date format*/ PublicBoolean 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); intYear = Integer.parseint (Matcher.group (3)); if(Day.equals (" to") &&(Month.equals ("4") || month. Equals ("6") || Month.equals ("9") ||Month.equals (" One") || Month.equals ("Geneva") || month. Equals (" .") ||Month.equals (" the"))) { return false;//Only 1,3,5,7,8,10,12 have } Else if(Month.equals ("2") || Month.equals (" Geneva")) { //Leap Year if(Year%4==0){ if(Day.equals (" -") || Day.equals (" to")){ return false; } Else{ return true; } } Else{ if(Day.equals (" in")|| Day.equals (" -")|| Day.equals (" to")){ return false; } Else{ return true; } } } Else{ return true; } } Else{ return false; } } Else{ return false; } }
Then put the following code in the onclick ():
Matcher = pattern.compile (date_pattern). Matcher (Birthday); // Birthday Validator Else if (! matcher.matches ()) { "Invalid birthday! " , Toast.length_short). Show (); }
Verify Android User input date