If the date is illegal, throw an exception to try {String date_str = "5555-22-33"; SimpleDateFormat format=new SimpleDateFormat ("Yyyy-mm-dd"); Format.setlenient (false);D ate Date = Format.parse (date_ STR);} catch (Exception ex) {ex.printstacktrace (); SYSTEM.OUT.PRINTLN ("date not valid");}
Description of the notes in the JDK:
/** * Specify whether or not date/time parsing are to be lenient. with * Lenient parsing, the parser could use heuristics to interpret inputs that * does not precisely match this objec T ' s format. With strict parsing, * Inputs must match this object ' s format. * * <p>this method is equivalent to the following call. * <blockquote><pre> * Getcalendar (). Setlenient (lenient) * </pre></blockquote > * * <p>this leniency value is overwritten by a call to {@link * #setCalendar (Java.util.Calendar ) Setcalendar ()}. * * @param lenient when {@code true}, parsing is lenient * @see java.util.calendar#setlenient (Boolean) */ Public void Setlenient (Boolean lenient) { calendar.setlenient (lenient); }
The date type in Java is quick to determine whether the dates are legitimate.