In my project I need to check if a date string evaluates to a proper Date object. I\'ve decided to allow yyyy-MM-dd, and Date formats [(year, month, date) and (year, month,
Just to show the final outcome, thanks to Csaba_H and Steve Kuo.
private Date parseDate(String date){
SimpleDateFormat[] possibleFormats = new SimpleDateFormat[] {
new SimpleDateFormat("yyyy-MM-dd"),
new SimpleDateFormat("yyyy,MM,dd"),
new SimpleDateFormat("yyyy,MM,dd,HH,mm") };
Date retVal = null;
for (SimpleDateFormat f: possibleFormats) {
f.setLenient(false);
try {
retVal = f.parse(date);
} catch (ParseException e) {}
}
return retVal;
}