Match date against multiple date formats

Discussion in 'Java' started by Aparna, Jun 14, 2007.

  1. Aparna

    Aparna Guest

    I need to match a given date against 5 different format. How can I do
    this ?

    The predefined date formats are :-
    Format 1 - yyyy MMM dd hh:mm:ss a
    Format 2 - yy-MM-dd hh:mm:ss
    Format 3 - MMM dd yyyy hh:mm
    Format 4 - dd MMM yyyy hh:mm
    Format 5 - dd-MM-yy hh:mm

    and If I get a date "2007 Jul 14 10:21:19 am", I need to pick the
    first option ie., Format 1.

    How can I go about this ? Is there any API which can determine the
    date format of a string? Any help is appreciated.

    -Aparna
    Aparna, Jun 14, 2007
    #1
    1. Advertising

  2. Here's a quick and dirty solution:

    SimpleDateFormat[] formatters = new
    SimpleDateFormat[5];
    formatters[0] = new SimpleDateFormat("yyyy MMM dd hh:mm:ss a");
    formatters[1] = new SimpleDateFormat("yy-MM-dd hh:mm:ss");
    formatters[2] = new SimpleDateFormat("MMM dd yyyy hh:mm");
    formatters[3] = new SimpleDateFormat("dd MMM yyyy hh:mm");
    formatters[4] = new SimpleDateFormat("dd-MM-yy hh:mm");


    String date = "07-07-14 10:21:19";
    boolean found = false;
    for (int i = 0; i < formatters.length; i++) {
    try {
    formatters.parse(date);
    System.out.println("Pattern " + (i+1));
    found = true;
    break;
    } catch (Exception e) {

    }
    }

    if (!found) {
    System.out.println("No format found.");
    }

    On Jun 14, 3:14 pm, Aparna <> wrote:
    > I need to match a given date against 5 different format. How can I do
    > this ?
    >
    > The predefined date formats are :-
    > Format 1 - yyyy MMM dd hh:mm:ss a
    > Format 2 - yy-MM-dd hh:mm:ss
    > Format 3 - MMM dd yyyy hh:mm
    > Format 4 - dd MMM yyyy hh:mm
    > Format 5 - dd-MM-yy hh:mm
    >
    > and If I get a date "2007 Jul 14 10:21:19 am", I need to pick the
    > first option ie., Format 1.
    >
    > How can I go about this ? Is there any API which can determine the
    > date format of a string? Any help is appreciated.
    >
    > -Aparna
    Mark Bryan Yu, Jun 14, 2007
    #2
    1. Advertising

  3. Aparna wrote:
    > I need to match a given date against 5 different format. How can I do
    > this ?
    >
    > The predefined date formats are :-
    > Format 1 - yyyy MMM dd hh:mm:ss a
    > Format 2 - yy-MM-dd hh:mm:ss
    > Format 3 - MMM dd yyyy hh:mm
    > Format 4 - dd MMM yyyy hh:mm
    > Format 5 - dd-MM-yy hh:mm
    >
    > and If I get a date "2007 Jul 14 10:21:19 am", I need to pick the
    > first option ie., Format 1.
    >
    > How can I go about this ? Is there any API which can determine the
    > date format of a string? Any help is appreciated.
    >
    > -Aparna
    >

    Last time I needed to do this, a very long time ago, the requirement was
    to accept a very wide variety of possible date formats (these included,
    but were not limited to, dates like 23Jan90, 1586, 55BC, 1Q/07) with the
    requirement that any valid date must be output in the format it had been
    input in and must be capable of being compared to any other date. I used
    a set of steps to recognize the formats:

    - made a copy of the input
    - modified the copy by replacing all digits with 9, all letters with X
    and multiple spaces with a single space
    - compared the string with a set of pattern strings until I got a match
    - used the matching pattern reference to extract ccyyddmm from the
    original input.
    - check that the ccyyddmm string was a valid date and the input format
    was permitted for the input field
    - store both the date and the pattern reference.

    It may be possible to use standard Java date formats as recognition
    patterns as well as conversion templates. However, pattern recognition
    may may be easier with the more basic Xs and 9s.


    --
    martin@ | Martin Gregorie
    gregorie. | Essex, UK
    org |
    Martin Gregorie, Jun 14, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martin Eyles

    Date Formats of Date in Database

    Martin Eyles, Mar 28, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    1,751
    Martin Eyles
    Mar 29, 2006
  2. thorsten
    Replies:
    1
    Views:
    446
  3. BogusException
    Replies:
    3
    Views:
    343
    BogusException
    Aug 16, 2006
  4. Replies:
    0
    Views:
    397
  5. Richard Hollenbeck

    convert multiple date formats and compare those dates

    Richard Hollenbeck, Feb 11, 2004, in forum: Javascript
    Replies:
    4
    Views:
    166
    Dr John Stockton
    Feb 12, 2004
Loading...

Share This Page