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