Function: Check whether the date is valid (mainly to determine whether the validity of a month's date exceeds the maximum date of the month)
Test data: 20070229 false
20070228 false
20040229 true
/**
* Check if the date is valid
*
* @ Param date the date to be checked
* @ Return: whether the date is valid
*/
Public static Boolean chkdateformat (string date ){
Try {
// If the input date is not 8 bits, the value is false.
If (null = date | "". Equals (date) |! Date. Matches ("[0-9] {8 }")){
Return false;
}
Int year = integer. parseint (date. substring (0, 4 ));
Int month = integer. parseint (date. substring (4, 6)-1;
Int day = integer. parseint (date. substring (6 ));
Calendar calendar = gregoriancalendar. getinstance ();
// When the calendar is in non-lenient mode, if any inconsistency exists in the calendar field, it throws an exception.
Calendar. setlenient (false );
Calendar. Set (calendar. year, year );
Calendar. Set (calendar. Month, month );
Calendar. Set (calendar. Date, Day );
// If the date is incorrect, an exception is thrown when this statement is executed.
Calendar. Get (calendar. year );
} Catch (illegalargumentexception e ){
Return false;
}
Return true;
}