I'm using the following code to categorize whether or not the data type of text input is a date:
private boolean isDate(String val)
{
//These are the only formats dates will have
String[] formatList = {"MM/dd/yyyy", "MM-dd-yyyy", "MMM/dd/yyyy", "dd-MMM-yyyy"};
SimpleDateFormat dateFormat = new SimpleDateFormat();
dateFormat.setLenient(false);
dateFormat.
//Loop through formats in formatList, and if one matches the string return true
for (String str:formatList)
{
try
{
dateFormat.applyPattern(str);
dateFormat.parse(val.trim());
} catch (ParseException pe)
{
continue;
}
return true;
}
return false;
}
This method is called in a switch statement along with other function like isNumber, isDatetime, isVarchar2, etc. When I pass the value '4/05/2013 23:54' which is a datetime, the SimpleDateFormat object successfully parses it. I assume this is because it performs a regex match. This means I have to call isDate after isDatetime or nothing will ever be categorized as a datetime. Is there a simple way to get SimpleDateFormat to strictly match (ie, choke when there are extra characters)?