If you got just two formats this will suffice. If there are more of them you might want to use recursion. If this is the case, please inform me, I'll show you how to do it.
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("EEEE, MMMM d, yyyy h:mm a");
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("EEEE, d MMMM, yyyy H:mm");
Date result = null;
while( logic statement){
try{
result = simpleDateFormat1.parse(dateAsString);
}catch (ParseException e){
result = simpleDateFormat2.parse(dateAsString);
}
//do whatever want with the result.
}
As the OP asked this is the recursion way. You can copy paste it, it runs. If you want Collection
of formats instead of an array you may want to use Iterator
instead of an index i
.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
public static Date parseDate(String dateAsString, SimpleDateFormat[] formats, int i) {
if (i == formats.length) {
return null;
}
try {
return formats[i].parse(dateAsString);
} catch (ParseException e) {
return parseDate(dateAsString, formats, i + 1);
}
}
public static void main(String[] args) {
SimpleDateFormat[] formats = { new SimpleDateFormat("EEEE, MMMM d, yyyy h:mm a"), new SimpleDateFormat("EEEE, d MMMM, yyyy H:mm"), new SimpleDateFormat("dd.MM.yy") };
String[] datesAsStrings = {"Sunday, November 15, 2015 7:00 PM", "Saturday, 14 November, 2015 22:04", "25.07.15", "this is NOT a date"};
for(String dateAsString :datesAsStrings){
System.out.println(parseDate(dateAsString, formats, 0));
}
}
}