Prev: TYPE_SCROLL_SENSITIVE
Next: Dates in Excel using POI
From: student4life on 28 Jun 2010 07:31 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.
From: Lew on 28 Jun 2010 08:21 On 06/28/2010 07:31 AM, 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. <http://java.sun.com/javase/6/docs/api/java/text/DateFormat.html> and <http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html> -- Lew
From: Arne Vajhøj on 28 Jun 2010 09:28 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
From: John B. Matthews on 28 Jun 2010 10:36 In article <24af335e-8113-466b-acab-ddc52e48cc01(a)y4g2000yqy.googlegroups.com>, student4life <student4lifer(a)gmail.com> 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>
From: Arne Vajhøj on 28 Jun 2010 15:26
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 |