content

Judge time format The format must be “YYYY-MM-DD” 2004-2-30 is invalid 2003-2-29 is invalid call method isLegalDate () implementation.

code

private static boolean isLegalDate(String sDate) {
    int legalLen = 10;
    if ((sDate == null) || (sDate.length() ! = legalLen)) {return false;
    }
 
    DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    try {
        Date date = formatter.parse(sDate);
        return sDate.equals(formatter.format(date));
    } catch (Exception e) {
        return false; }}Copy the code