2

I have the following SimpleDateFormat

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE, MMMM d, yyyy h:mm a");

which works fine for the following date

Sunday, November 15, 2015 7:00 PM

The format of the date I'm parsing is not always like that. Sometimes it would be look like below

Saturday, 14 November, 2015 22:04

How can parse both of them successfully?

Arya
  • 8,473
  • 27
  • 105
  • 175
  • 4
    Use 2 `SimpleDateFormat`? There is no magic here. – Tunaki Nov 15 '15 at 21:33
  • How would I detect which to use reliably? The program would not know which date format would show up – Arya Nov 15 '15 at 21:34
  • Apache Commons has a utility which you pass a `String` and an array of `DateFormat`s to, it then loops through these and attempts to parse the `String` against the `DateFormat`s until either one is found which works or it runs through them all and returns `null`, maybe you could do the same thing – MadProgrammer Nov 15 '15 at 21:35
  • *"How would I detect which to use reliably? The program would not know which date format would show up"* - [`DateFormat#parse`](https://docs.oracle.com/javase/8/docs/api/java/text/DateFormat.html#parse-java.lang.String-) throws a `ParseException` when it can't parse the `String` with the given format – MadProgrammer Nov 15 '15 at 21:37
  • Maybe something like [this](http://stackoverflow.com/questions/25298524/comparing-dates-not-working/25298682#25298682) – MadProgrammer Nov 15 '15 at 21:46

1 Answers1

6

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));
        }
    }
}
Yoda
  • 17,363
  • 67
  • 204
  • 344