Java determines whether a string conforms to the YYYYMMDD date format

Source: Internet
Author: User

Java determines whether a string conforms to the YYYYMMDD date format

Code:

    /*** Determine if the format of the parameter is a valid date string in the format "YYYYMMDD" **/       Public Static Booleanisvaliddate (String str) {Try {              if(str! =NULL&&!str.equals ("")) {                  if(str.length () = = 8) {                      //leap Year Flag                    BooleanIsleapyear =false; String Year= str.substring (0, 4); String Month= Str.substring (4, 6); String Day= Str.substring (6, 8); intVyear =Integer.parseint (year); //Judging whether the year is legal                    if(Vyear < 1900 | | vyear > 2200) {                          return false; }                      //determine if a leap year                    if(vyear% 4 = = 0 && vyear%! = 0 | | vyear% 400 = 0) {isleapyear=true; }                      //Judging the month//1. Judging the month                    if(Month.startswith ("0") ) {String units4month= Month.substring (1, 2); intVunits4month =Integer.parseint (Units4month); if(Vunits4month = = 0) {                              return false; }                          if(Vunits4month = = 2) {                              //get the number of days in February                            intVdays4february =Integer.parseint (day); if(isleapyear) {if(Vdays4february > 29)                                      return false; } Else {                                  if(Vdays4february > 28)                                      return false; }                          }                      } Else {                          //2. Determine if a month that is not 0 is legal                        intVmonth =Integer.parseint (month); if(Vmonth! = && Vmonth! = && Vmonth! = 12) {                              return false; }                      }                      //Judging Date//1. Date of Judgment                    if(Day.startswith ("0") ) {String units4day= Day.substring (1, 2); intVunits4day =Integer.parseint (Units4day); if(Vunits4day = = 0) {                              return false; }                      } Else {                          //2. Determine if the date of the non-0 is legal                        intVDay =Integer.parseint (day); if(VDay < | | VDay > 31) {                              return false; }                      }                      return true; } Else {                      return false; }              } Else {                  return false; }          } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); return false; }      }  

Java determines whether a string conforms to the YYYYMMDD date format

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.