If you unknown pattern format you can use something like this
DateTimeFormatter formatter1 = DateTimeFormat.forPattern("d/MM/YY")
.withLocale(Locale.UK);
DateTimeFormatter formatter2 = DateTimeFormat.forPattern("m:d:YYYY")
.withLocale(Locale.UK);
...
DateTimeFormatter formatterN = DateTimeFormat.forPattern(...
String stringDate = "08:18:2012";
LocalDate date;
try {
date = formatter1.parseLocalDate(stringDate);
} catch (Exception exp) {
try {
date = formatter2.parseLocalDate(stringDate);
} catch (Exception exp) {
...
date = formatterN.parseLocalDate(stringDate);
}
}
OR using List:
List<DateTimeFormatter> formatterList = new ArrayList<>();
formatterList.add(DateTimeFormat.forPattern("d/MM/YY")
.withLocale(Locale.UK));
formatterList.add(DateTimeFormat.forPattern("m:d:YYYY")
.withLocale(Locale.UK));
...
formatterList.add(DateTimeFormat.forPattern(...
String stringDate = "08:18:2012";
LocalDate date;
for(DateTimeFormatter formatter : formatterList) {
try {
return formatter.parseLocalDate(stringDate);
} catch (Exception exp) {
}
}
But it's impossible if you have pattern like "d/MM/YY" and "MM/d/YY", because you can recognize what string "01/01/15" means (where day and where month). Only if you have a lot of strings with one pattern you can statistically undestand what is day and what is month (month never be more then 12).