Question

What's the nicest way to parse a date that can be in one of the following formats

 "dd-MM-yyyy HH:mm"
 "dd/MM/yyyy HH:mm"
 "dd.MM.yyyy HH:mm"

without creating 3 SimpleDateFormats and parsing against each one.

Thanks

Was it helpful?

Solution

It's probably easiest to "tweak" the source string into a canonical format:

if (text.length() == 16)
{
    if ((text.charAt(2) == '/' && text.charAt(5) == '/') ||
        (text.charAt(2) == '.' && text.charAt(5) == '.'))
    {
        text = text.substring(0, 2) + "-" + text.substring(3, 5) 
           + "-" + text.substring(6);
    }
}

Then use the format string using "-".

Note that this is very specific, only replacing exactly the characters you're interested in, to avoid unwanted side-effects.

OTHER TIPS

Could you run two replace operations first, so that you reduce all three formats to a single one?

You may use Apache commons lang DateUtils.parseDate

import java.text.ParseException;
import org.apache.commons.lang.time.DateUtils;

public class Test {

public static void main(String[] args) throws ParseException {

    String[] values = new String[]{"31-12-2009 12:00", "31/12/2009 12:00", "31.12.2009 12:00"};
    String[] parsePatterns = new String[]{"dd-MM-yyyy HH:mm", "dd/MM/yyyy HH:mm", "dd.MM.yyyy HH:mm"};

    for (String value : values) {
        System.out.println(DateUtils.parseDate(value, parsePatterns));
    }
}
}

Well, internally it creates SimpleDateFormats, but whats wrong with that?

how about a regex:

"\\d\\d[./-]\\d\\d[./-]\\d\\d\\d\\d \\d\\d:\\d\\d"

In code this would mean something like this:

Pattern pattern = 
Pattern.compile("(\\d\\d)([./-])(\\d\\d)([./-])(\\d\\d\\d\\d) (\\d\\d):(\\d\\d)");

Matcher matcher = 
pattern.matcher("31-07-1983 15:30");

if (matcher.find() && matcher.group(2).equals(matcher.group(4))) {
  int day = Integer.parseInt(matcher.group(1));
  int month = Integer.parseInt(matcher.group(3));
  int year = Integer.parseInt(matcher.group(5));
  int hour = Integer.parseInt(matcher.group(6));
  int minute = Integer.parseInt(matcher.group(7));
} 

Doing it yourself with a regular expression:

public class SpecialDateFormat
{
    private final static Pattern PATTERN = Pattern.compile("(\\d{2})[\\.\\/\\-](\\d{2})[\\.\\/\\-](\\d{4}) (\\d{2}):(\\d{2})");

    public static Date parse(String text) throws ParseException {
        Matcher m = PATTERN.matcher(text);
        if (m.matches()) {
            int dd = Integer.parseInt(m.group(1));
            int mm = Integer.parseInt(m.group(2));
            int yy = Integer.parseInt(m.group(3));
            int hh = Integer.parseInt(m.group(4));
            int mi = Integer.parseInt(m.group(5));

            // NOTE: Checking if values are in valid ranges omitted

            Calendar cal = Calendar.getInstance();
            cal.set(yy, mm - 1, dd, hh, mi, 0);

            return cal.getTime();
        }
        else {
            throw new ParseException("Unparseable date: " + text, 0);
        }
    }
}

Note however that this does allow mixing different separators, e.g. "17-09/2009 12:00" would be allowed.

ParseExact can take an array of formats. You still have to specify all formats, but it's a single operation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top