parsing dates in a string

Discussion in 'Java' started by student4life, Jun 28, 2010.

  1. student4life

    student4life Guest

    Could someone show me the best way to parse the first occurrence of
    dates (could be in different date formats, MM/dd/yy, yyyy/MM/dd, etc.)
    in a string preferably without using regular expression? Thank you.
     
    student4life, Jun 28, 2010
    #1
    1. Advertising

  2. student4life

    Arne Vajhøj Guest

    On 28-06-2010 07:31, student4life wrote:
    > Could someone show me the best way to parse the first occurrence of
    > dates (could be in different date formats, MM/dd/yy, yyyy/MM/dd, etc.)
    > in a string preferably without using regular expression? Thank you.


    DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
    Date d = df.parse(s);

    etc..

    Note that if the format includes names instead of numbers, then it
    is locale specific.

    Arne
     
    Arne Vajhøj, Jun 28, 2010
    #2
    1. Advertising

  3. student4life

    Stefan Ram Guest

    student4life <> writes:
    >Could someone show me the best way to parse the first occurrence of
    >dates (could be in different date formats, MM/dd/yy, yyyy/MM/dd, etc.)
    >in a string preferably without using regular expression? Thank you.


    Here is a test string for »yyyy-mm-dd«:

    String Result should be

    1241241-41051040-10-412010-10-1091 2010-10-10
     
    Stefan Ram, Jun 28, 2010
    #3
  4. In article
    <>,
    student4life <> wrote:

    > Could someone show me the best way to parse the first occurrence of
    > dates (could be in different date formats, MM/dd/yy, yyyy/MM/dd, etc.)
    > in a string preferably without using regular expression? Thank you.


    Armed with Lew's and Arne's helpful suggestions, here is a slightly more
    elaborate example that might be informative:

    <http://groups.google.com/group/comp.lang.java.programmer/msg/d9f7a770213
    9b48f>

    Degeneracy, in the sense of indistinguishability, may be a problem when
    parsing the same string using very different date formats. You may need
    to flag dates that appear to parse correctly with more than one format.

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Jun 28, 2010
    #4
  5. student4life

    Tom Anderson Guest

    On Mon, 28 Jun 2010, student4life wrote:

    > Could someone show me the best way to parse the first occurrence of
    > dates (could be in different date formats, MM/dd/yy, yyyy/MM/dd, etc.)
    > in a string preferably without using regular expression?


    A regular expression is far and away the best way of doing this. Why don't
    you want to use one?

    tom

    --
    In case you don't know what CROWDSOURCING is, it's a stomach-churning
    new media term obviously invented by a bastard made of piss. -- Charlie
    Brooker
     
    Tom Anderson, Jun 28, 2010
    #5
  6. student4life

    Arne Vajhøj Guest

    On 28-06-2010 11:30, Tom Anderson wrote:
    > On Mon, 28 Jun 2010, student4life wrote:
    >> Could someone show me the best way to parse the first occurrence of
    >> dates (could be in different date formats, MM/dd/yy, yyyy/MM/dd, etc.)
    >> in a string preferably without using regular expression?

    >
    > A regular expression is far and away the best way of doing this. Why
    > don't you want to use one?


    If the position of the date is unknown, then regex is a very good
    choice for finding candidates.

    DateFormat.parse should still be used to verify, because regex
    is not the right tool to discard February 30th etc..

    Arne
     
    Arne Vajhøj, Jun 28, 2010
    #6
  7. student4life

    Tom Anderson Guest

    On Mon, 28 Jun 2010, Arne Vajh?j wrote:

    > On 28-06-2010 11:30, Tom Anderson wrote:
    >> On Mon, 28 Jun 2010, student4life wrote:
    >>> Could someone show me the best way to parse the first occurrence of
    >>> dates (could be in different date formats, MM/dd/yy, yyyy/MM/dd, etc.)
    >>> in a string preferably without using regular expression?

    >>
    >> A regular expression is far and away the best way of doing this. Why
    >> don't you want to use one?

    >
    > If the position of the date is unknown, then regex is a very good
    > choice for finding candidates.
    >
    > DateFormat.parse should still be used to verify, because regex
    > is not the right tool to discard February 30th etc..


    I strongly agree with this.

    I suppose a way you could do it without a regexp would be:

    public Date findDateInString(String s) {
    for (int i = 0; i < s.length(); ++i) {
    Date d = DATE_FORMAT.parse(s.substring(i));
    if (d != null) return d;
    }
    throw new IllegalArgumentException("no date found in string: " + s);
    }

    Anyone who does that needs a beating, though.

    tom

    --
    Fitter, Happier, More Productive.
     
    Tom Anderson, Jun 28, 2010
    #7
  8. student4life

    Lew Guest

    student4life wrote:
    >>>> Could someone show me the best way to parse the first occurrence of
    >>>> dates (could be in different date formats, MM/dd/yy, yyyy/MM/dd, etc.)
    >>>> in a string preferably without using regular expression?

    >


    Tom Anderson wrote:
    >>> A regular expression is far and away the best way of doing this. Why
    >>> don't you want to use one?

    >


    Arne Vajhøj wrote:
    >> If the position of the date is unknown, then regex is a very good
    >> choice for finding candidates.
    >>
    >> DateFormat.parse should still be used to verify, because regex
    >> is not the right tool to discard February 30th etc..

    >


    Tom Anderson wrote:
    > I strongly agree with this.
    >
    > I suppose a way you could do it without a regexp would be:
    >
    > public Date findDateInString(String s) {
    >         for (int i = 0; i < s.length(); ++i) {
    >                 Date d = DATE_FORMAT.parse(s.substring(i));
    >                 if (d != null) return d;
    >         }
    >         throw new IllegalArgumentException("no date found in string: " + s);
    >
    > }
    >
    > Anyone who does that needs a beating, though.
    >


    To really earn that beating, use the ParsePosition forms of 'parse()'
    in that loop:
    <http://java.sun.com/javase/6/docs/api/java/text/
    DateFormat.html#parse(java.lang.String, java.text.ParsePosition)>
    et seq.

    We still need to hear from the OP why they wish to avoid regexes.

    --
    Lew
     
    Lew, Jun 28, 2010
    #8
    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. David Lozzi

    Dates dates dates dates... SQL and ASP.NET

    David Lozzi, Sep 29, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    869
    Rob Schieber
    Sep 30, 2005
  2. PW

    Dates! Dates! Dates!

    PW, Aug 7, 2004, in forum: ASP General
    Replies:
    4
    Views:
    199
    Mark Schupp
    Aug 9, 2004
  3. Replies:
    1
    Views:
    206
    Jano Svitok
    Jul 17, 2007
  4. kellygreer1

    RFC-822 dates into Ruby dates

    kellygreer1, Jun 8, 2008, in forum: Ruby
    Replies:
    1
    Views:
    201
    Eric I.
    Jun 8, 2008
  5. Dr J R Stockton

    FAQ Dates; Opera Dates.

    Dr J R Stockton, Jun 18, 2007, in forum: Javascript
    Replies:
    0
    Views:
    163
    Dr J R Stockton
    Jun 18, 2007
Loading...

Share This Page