For that to work you'd basically have to parse the date before parsing the date, which is tricky
.
I try to constrain the date formats I have to use. But if I was trying to do this, here's the approach I use:
- Decide on all the formats I want to use, in order of precedence
- Set up a list of SimpleDateFormat objects using those formats
- Iterate through the list, trying to parse the date using each one in turn (obviously you need to catch and ignore any parsing exceptions within the loop). The first one that doesn't throw an exception is the output. You also need to decide what to do if all of them fail (either a default date or throw a proper exception).