I have the code below, and it works pretty well, unless you enter something like 2/2/2011, you get the error "Document date is not a valid date." I would expect him to say: "The date of the document should be in the format MM / DD / YYYY."
Why newDate = dateFormat.parse(date);doesn't the string recognize this?
private String isValidDate(String date) {
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Date newDate = null;
if(!date.equals("mm/dd/yyyy")) {
try {
newDate = dateFormat.parse(date);
} catch(ParseException e) {
return "The Document Date needs to be in the format MM/DD/YYYY\n";
}
if(!dateFormat.format(newDate).toUpperCase().equals(date.toUpperCase())) {
return "The Document Date is not a valid date\n";
}
return "true";
} else {
return "- Document Date\n";
}
}
EDIT: I am trying to enforce strict adherence to the MM / DD / YYYY format. How can I change the code so that if the user enters "2/2/2011", he will display a message: "The date of the document should be in the format MM / DD / YYYY"?
source
share