I would like to know how to check if a user-supplied input date is valid or not.
I would like to know how to check if a user-supplied input date is valid or not.
You can use this solution:
public class VerificarData {
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
sdf.setLenient(false);
String dataHoje = "08/12/2014";
try {
Date data = sdf.parse(dataHoje);
System.out.println("A data é válida.");
} catch(ParseException e) {
System.out.println("A data é inválida.");
}
}
}
This solution I use in an application here and it works, I got it in the GUJ but I do not remember the link, so the credit is from whoever posted it.
The setLenient serves to say that there can be no error in the String, if you put for example 10/13/2014 it will return error.