I am trying to validate date inputs, and I only want them to pass if the day and the month are always two digits. So far I've been doing this with SimpleDateFormat:
SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
df.setLenient(false);
try
{
df.parse("10/1/1987");
df.parse("1/1/1987");
df.parse("1/10/1987");
df.parse("1/1/19");
}
catch (ParseException e)
{
e.printStackTrace();
}
All of those cases are passing though, and I don't want any of them to pass.
Is there an easy way to fix this, or will I have to tokenize the string on backslashes first with something like:
String[] dateParts = date.split("/");
if (dateParts.length != 3 && dateParts[0].length() != 2 && dateParts[1].length() != 2 && dateParts[2].length() != 4)
System.out.println("Invalid date format");