Parsing german dates from unformatted input

In a recent case I needed to combine many date values out of a bunch of XForms documents. The forms had changed over time in a way that there was no date input at the beginning, so date values could be typed in freely. That ended up in values like “07.07.2012″, “7.7.2012″, “07. 07.2012″, “07.07.12″ and so on. Then there were also erroneously typed spaces. Later on, the input was marked as date type and standard input selectors were used, dates from that moment on were saved as “2012-07-07T12:00:00″ (UTC Time Format as specified in ISO 8601).

So, what I needed was a way to get the date formatted in a standard way as “dd.MM.yyyy” (first of the examples above) or the original value back for further error handling. I did not want to include any further libraries as the project is distributed via web and I want to keep the dependencies as little as possible. So I came up with a method to parse the dates from a combination of SimpleDateFormat, that involve the different possibilities:

private String tryConvertDate(String date) {
        String value = "";
        if (date != null) {
            value = date;
            // remove spaces
            date = date.replaceAll(" ", "");
            // remove time zone information at the end of the string as we are only in one timezone and interested in the date
            date = date.replace("\\+.{5}$","");
            List<SimpleDateFormat> dfs = new ArrayList<SimpleDateFormat>();
            // add english date format as first, as it comes from date input and is strictly defined and therefore needs to be parsed first
            dfs.add(new SimpleDateFormat("yyyy-MM-dd"));
            // iterate the different possible formats for each part of the date and finally create a SimpleDateFormat for it
            for (String day : Arrays.asList("d", "dd")) {
                for (String month : Arrays.asList("M", "MM")) {
                    for (String year : Arrays.asList("yy", "yyyy")) {
                        for (String devider : Arrays.asList(".", "-", "")) {
                            dfs.add(new SimpleDateFormat(day + devider + month + devider + year)); // german style dates
            Date tempDate = null;
            // iterate over the create DateFormats and test the date. If DateFormat fits, set tempDate to the parsed Date
            for (SimpleDateFormat df : dfs) {
                if (tempDate == null) {
                    try {
                        tempDate = df.parse(date);
                    } catch (ParseException e) {
            // if we found a value, format the correct Date String
            if (tempDate != null) {
                SimpleDateFormat outputFormat = new SimpleDateFormat("dd.MM.yyyy");
                value = outputFormat.format(tempDate);
        return value;

May that be of use to someone else or as a starting point for your similar problem.

Related Posts

No Comments

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment