Verify Android User input date

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.