0

I am grabbing dates from Various API's like G+ FB and Skype, I have one following function which I got from here

private static final Map<String, String> DATE_FORMAT_REGEXPS = new HashMap<String, String>() {{
        put("^\\d{8}$", "yyyyMMdd");
        put("^\\d{1,2}-\\d{1,2}-\\d{4}$", "dd-MM-yyyy"); //Aug 24, 1990
        put("^\\d{4}-\\d{1,2}-\\d{1,2}$", "yyyy-MM-dd");
        put("^\\d{1,2}/\\d{1,2}/\\d{4}$", "MM/dd/yyyy");
        put("^\\d{4}/\\d{1,2}/\\d{1,2}$", "yyyy/MM/dd");
        put("^\\d{1,2}\\s[a-z]{3}\\s\\d{4}$", "dd MMM yyyy");
        put("^\\d{1,2}\\s[a-z]{4,}\\s\\d{4}$", "dd MMMM yyyy");
        put("^\\d{12}$", "yyyyMMddHHmm");
        put("^\\d{8}\\s\\d{4}$", "yyyyMMdd HHmm");
        put("^\\d{1,2}-\\d{1,2}-\\d{4}\\s\\d{1,2}:\\d{2}$", "dd-MM-yyyy HH:mm");
        put("^\\d{4}-\\d{1,2}-\\d{1,2}\\s\\d{1,2}:\\d{2}$", "yyyy-MM-dd HH:mm");
        put("^\\d{1,2}/\\d{1,2}/\\d{4}\\s\\d{1,2}:\\d{2}$", "MM/dd/yyyy HH:mm");
        put("^\\d{4}/\\d{1,2}/\\d{1,2}\\s\\d{1,2}:\\d{2}$", "yyyy/MM/dd HH:mm");
        put("^\\d{1,2}\\s[a-z]{3}\\s\\d{4}\\s\\d{1,2}:\\d{2}$", "dd MMM yyyy HH:mm");
        put("^\\d{1,2}\\s[a-z]{4,}\\s\\d{4}\\s\\d{1,2}:\\d{2}$", "dd MMMM yyyy HH:mm");
        put("^\\d{14}$", "yyyyMMddHHmmss");
        put("^\\d{8}\\s\\d{6}$", "yyyyMMdd HHmmss");
        put("^\\d{1,2}-\\d{1,2}-\\d{4}\\s\\d{1,2}:\\d{2}:\\d{2}$", "dd-MM-yyyy HH:mm:ss");
        put("^\\d{4}-\\d{1,2}-\\d{1,2}\\s\\d{1,2}:\\d{2}:\\d{2}$", "yyyy-MM-dd HH:mm:ss");
        put("^\\d{1,2}/\\d{1,2}/\\d{4}\\s\\d{1,2}:\\d{2}:\\d{2}$", "MM/dd/yyyy HH:mm:ss");
        put("^\\d{4}/\\d{1,2}/\\d{1,2}\\s\\d{1,2}:\\d{2}:\\d{2}$", "yyyy/MM/dd HH:mm:ss");
        put("^\\d{1,2}\\s[a-z]{3}\\s\\d{4}\\s\\d{1,2}:\\d{2}:\\d{2}$", "dd MMM yyyy HH:mm:ss");
        put("^\\d{1,2}\\s[a-z]{4,}\\s\\d{4}\\s\\d{1,2}:\\d{2}:\\d{2}$", "dd MMMM yyyy HH:mm:ss");
    }};



    public String determineDateFormat(String dateString) {
        for (String regexp : DATE_FORMAT_REGEXPS.keySet()) {
            if (dateString.toLowerCase().matches(regexp)) {
                return DATE_FORMAT_REGEXPS.get(regexp);
            }
        }
        return "Unknown Format"; // Unknown format.
    }

It works for most of the date formats, albeit if I encounter a date like: Aug 24, 1990. I want to know which date format is this? MMM DD YYYY or what? Also any hints so that I can add this to the already specified regex?

User3
  • 2,465
  • 8
  • 41
  • 84
  • `put("^[a-z]{3} \\d[1,2}, \\d{4}$", "MMM dd, yyyy")`; search the java Pattern API doc and you'll find a description of regex. Map is a bit of abused data structure here; and `^...$` could be `...`. A `List>` or `String[][]` would do. Also you could immediately return a Date, or null, or a LinkedSet of dates (matching dates in order of retrieval) using parse. – Joop Eggen Aug 01 '14 at 11:20
  • I am not so good at regex, learning from Learn reg ex the hard way. Let me try your suggestion and thanks a lot :) – User3 Aug 01 '14 at 11:28
  • It gives me a java.util.regex.PatternSyntaxException: Missing closing bracket in character class near index 25: – User3 Aug 01 '14 at 11:31
  • 1
    Try this regex instead : `^[a-z]{3}\\s\\d{2},\\s\\d{4}$` – SamYonnou Aug 01 '14 at 12:28
  • Sam post that as an answer, Ill accept! – User3 Aug 01 '14 at 12:34
  • @SamYonnou thanks for correcting, though better keep `\\d{1,2}` which means also 1 digit is accepted for the day, "June 1, 2020". MMM, also maybe `\\d{1,2}(th|nd|rd)?`. – Joop Eggen Aug 01 '14 at 14:13
  • @JoopEggen I see however if this is meant to correspond to the date format `dd` (used in something like a `SimpleDateFormatter`) I am pretty sure that it must match `\\d{2}`. – SamYonnou Aug 01 '14 at 14:49

2 Answers2

1

Pattern for Aug 24, 1990 is "MMM dd, yyyy" with en Locale.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
0

Have you tried using a specific library for this purpose instead? I find Natty to be very useful for me when I am processing random date formats from external APIs.

https://github.com/joestelmach/natty

You can try it out on the site too in an interactive form to see what it can and cant process before you decide to integrate it with your code.

http://natty.joestelmach.com/try.jsp

dectarin
  • 986
  • 5
  • 15
  • I am skeptical to use a library, I dont want to increase the size of my .apk because I already have so many SDK's clubbed. Isn't there an easier way? – User3 Aug 01 '14 at 11:15
  • Hmm, if you dont want to use a library then I guess you will just need to keep adding more and more regular expressions to the list as the need comes up. "MMM dd, yyyy" is the correct format for your return string anyway but I will leave the regexp to you as its not my strong suit! – dectarin Aug 01 '14 at 11:27