Hey Im using the code below to parse dates in different formats.
The trubel is, it takes the first format and parses the date even if it doesn't fit.
public static Date parseDate(String date) {
date = date.replaceAll("[-,.;:_/&()+*# ']", "-");
System.out.print(date);
List<String> formatStrings = Arrays.asList("d-M-y","y-M-d");
for (String formatString : formatStrings) {
try {
System.out.println(new SimpleDateFormat(formatString).parse(date));
return new SimpleDateFormat(formatString).parse(date);
} catch (Exception e) {
}
}
return null;
}
Console:
1994-02-13
Wed Jul 18 00:00:00 CEST 2018
OR
13-02-1994
Sun Feb 13 00:00:00 CET 1994
So I don't get why does the first format allways parse?