I am trying to specify a function for parsing a date in String
form to a Date
.
String startDate = "14-08-2014";
String endDate = "21/08/2020";
String middleDate = "Sep 27, 2018";
SimpleDateFormat sdf[] = new SimpleDateFormat[] {new SimpleDateFormat("dd/MM/yyyy"), new SimpleDateFormat("dd-MM-yyyy"), new SimpleDateFormat("MMM dd, yyyy") };
Date date = null;
for (DateFormat formatter : sdf) {
try {
date = formatter.parse(middleDate);
break;
} catch (ParseException e) {
}
System.out.println(date);
It works for two formats except the last one. If it uses middleDate
it finishes with Date
equal to null
. Can anybody tell me what is wrong?