parsing dates in a string

S

student4life

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.
 
A

Arne Vajhøj

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
 
S

Stefan Ram

student4life said:
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
 
J

John B. Matthews

student4life said:
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.
 
T

Tom Anderson

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
 
A

Arne Vajhøj

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
 
T

Tom Anderson

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
 
L

Lew

Tom said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top