I am supporting a common library at work that performs many checks of a given string to see if it is a valid date. The Java API, commons-lang library, and JodaTime all have
I think that the better way to know if a certain date is valid is defining a method like:
public static boolean isValidDate(String input, String format) {
boolean valid = false;
try {
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
String output = dateFormat.parse(input).format(format);
valid = input.equals(output);
} catch (Exception ignore) {}
return valid;
}
On one hand the method checks the date has the correct format , and on the other hand checks the date corresponds to a valid date . For example, the date "2015/02/29" will be parsed to "2015/03/01", so the input and output will be different, and the method will return false.