Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Parsing "February 24th, 2006" to java.util.Date

Reply
Thread Tools

Parsing "February 24th, 2006" to java.util.Date

 
 
stevengarcia@yahoo.com
Guest
Posts: n/a
 
      03-28-2006
how does one write a SimpleDateFormat pattern to take into account the
"th" or the "nd" that might be present on any date?

March 1st, 2006
March 2nd, 2006
March 3rd, 2006
March 4th, 2006

I'm not sure how to write a mask that can take into acct "st", "nd",
"rd", "th".

Thanks for your help.

 
Reply With Quote
 
 
 
 
Dave Mandelin
Guest
Posts: n/a
 
      03-28-2006
I don't think SimpleDate format can do it. I'd use a regexp to remove
those characters.

--
Need to get from a Foo object to a Bar object in Java?
Ask Prospector: http://snobol.cs.berkeley.edu
Want to play tabletop RPGs over the internet?
Check out Koboldsoft RPZen: http://www.koboldsoft.com

 
Reply With Quote
 
 
 
 
stevengarcia@yahoo.com
Guest
Posts: n/a
 
      03-28-2006
Anyone other takers?

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      03-28-2006
On 28 Mar 2006 13:46:56 -0800, wrote, quoted or
indirectly quoted someone who said :

>Anyone other takers?


// Parsing a Date of the form: "February 24th, 2006"

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class ParseDate
{
private static final SimpleDateFormat pattern = new
SimpleDateFormat( "MMM dd'th', yyyy" );

/**
* test harness
*
* @param args not used
*/
public static void main ( String[] args )
{



String dateString = "February 24th, 2006";
int where;
if ( (where = dateString.indexOf( "st," ) ) >= 0 )
{
dateString = dateString.substring( 0, where)
+ "th,"
+ dateString.substring( where + 3 );
}
else if ( (where = dateString.indexOf( "nd," ) ) >= 0 )
{
dateString = dateString.substring( 0, where)
+ "th,"
+ dateString.substring( where + 3 );
}
Date d = null;
try
{
d = pattern.parse( dateString );
}
catch ( ParseException e )
{
System.err.println( "oops:" + dateString );
}

System.out.println( d );

}
}

With JDK 1.5 you could use String.replace( "nd," ,"th," );
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Twisted
Guest
Posts: n/a
 
      03-28-2006
S'funny you should pick my 30th birthday as your example date (in the
Subject)...

--
I am the terror that flaps in the net!
I am the broken software with the awful user interface that the boss
forces everyone to use!
I am TWISTED!

 
Reply With Quote
 
James McGill
Guest
Posts: n/a
 
      03-29-2006
On Tue, 2006-03-28 at 22:25 +0000, Roedy Green wrote:
> On 28 Mar 2006 13:46:56 -0800, wrote, quoted or
> indirectly quoted someone who said :
>
> >Anyone other takers?


> With JDK 1.5 you could use String.replace( "nd," ,"th," );


Localizing it to handle e.g., "-ieme, -ere", or "-zig"... seems like
there's a case to be made for I18n-ized ordinal number parsing...
Hard-coding strings for "-st", "-nd", "-rd", "-th" just smells bad, in a
language that puts such emphasis on i18n.

 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      03-29-2006

"James McGill" <> wrote in message
news: ...
> On Tue, 2006-03-28 at 22:25 +0000, Roedy Green wrote:
>> On 28 Mar 2006 13:46:56 -0800, wrote, quoted or
>> indirectly quoted someone who said :
>>
>> >Anyone other takers?

>
>> With JDK 1.5 you could use String.replace( "nd," ,"th," );

>
> Localizing it to handle e.g., "-ieme, -ere", or "-zig"... seems like
> there's a case to be made for I18n-ized ordinal number parsing...
> Hard-coding strings for "-st", "-nd", "-rd", "-th" just smells bad, in a
> language that puts such emphasis on i18n.


In some languages, the entire word is changed when going from number to
ordinal, rather than just having a suffix added. It's like how the word
"one" changes to "first" in English (note that the two words have zero
letters in common).

So yeah, this is a non-trivial problem, and it'd probably be a great
boon to programmers if a standardized i18n API call existed for this. But
the syntax wouldn't be as simple as "MMM dd[ordinal-suffix], yyyy", but
rather, something like "MMM
[pure-ordinal-or-number-followed-by-ordinal-suffix], yyyy".

- Oliver

 
Reply With Quote
 
Twisted
Guest
Posts: n/a
 
      03-29-2006
Even though "first" is utterly different from "one", "1st" is just "1"
with a suffix.

RFE: add getSuffixFor(int) and getWordFor(int) to Locale? Typically,
there'll be some special cases for small enough integers (and an
illegal argument exception if argument <= 0?) and a simple algorithm
for larger integers. (In the case of English, starting at 20.) The
English algorithm for suffixes is especially simple, as it's just

if (arg > 10 && arg < 14) return "th";
switch (arg%10) {
case 1:
return "st";
case 2:
return "nd";
case 3:
return "rd";
default:
return "th";
}

(The only special cases are 11th, 12th, and 13th instead of 11st, 12nd,
and 13rd.)

The word one in English is similar -- you special-case 11, 12, and 13
("eleventh, twelfth, thirteenth" -- note you can't just add "th" or you
get "twelveth" for 12), and for the rest, you turn the LSD into an
ending "-first", "-second", "-third", or "-" + number's name + "th",
and the remaining digits into a beginning, e.g. "three hundred and
seventy", generating e.g. "three hundred and seventh-sixth".

Doing this for other languages is left as an exercise for the reader.


--
I am the terror that flaps in the net!
I am the leaky faucet in the kitchen of crime!
I am TWISTED!

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      03-29-2006
On Wed, 29 Mar 2006 20:40:30 GMT, "Oliver Wong" <>
wrote, quoted or indirectly quoted someone who said :

> So yeah, this is a non-trivial problem, and it'd probably be a great
>boon to programmers if a standardized i18n API call existed for this. But
>the syntax wouldn't be as simple as "MMM dd[ordinal-suffix], yyyy", but
>rather, something like "MMM
>[pure-ordinal-or-number-followed-by-ordinal-suffix], yyyy".


this is related to the problem of expressing numbers in words.

See http://mindprod.com/applets/inwords.html

It handles English ordinals in words.

Ordinals are used much less frequently than I remember them being used
as a child. Perhaps the irregularity discouraged their use in
computers.


--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      03-29-2006
"Twisted" <> wrote in message
news: oups.com...
>
> Even though "first" is utterly different from "one", "1st" is just "1"
> with a suffix.


Yeah, my point was that English (and most Latin/European languages) have
this "feature" that you can add a suffix to a arabic numeral (e.g. '1', '2',
'3') to turn them into ordinals (e.g. '1st', '2nd', '3rd'), but this is not
true for ALL languages.

Then I tried to give an analogy, but took an example from English.
Admittedly, that might be confusing, but I felt if I had used any other
language, I could not expect most readers here to relate to the example.

>
> RFE: add getSuffixFor(int) and getWordFor(int) to Locale?


Would getSuffixFor() return null, or throw an exception, for a Locale
for which these concepts of suffix don't exist?

Also, with "getWordFor(int)", there exists some languages where "the
word for a number" changes depending on what you are counting. For example,
in French, you might say "un homme" to mean "one man", but "une femme" to
mean "one woman". The word varies depending on the gender of the thing you
are counting. In Japanese, you vary the word depending on whether you're
counting something round, something flat, something pointy, etc.

- Oliver

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What libraries should I use for MIME parsing, XML parsing, and MySQL ? John Levine Ruby 0 02-02-2012 11:15 PM
[ANN] Parsing Tutorial and YARD 1.0: A C++ Parsing Framework Christopher Diggins C++ 0 07-09-2007 09:01 PM
[ANN] Parsing Tutorial and YARD 1.0: A C++ Parsing Framework Christopher Diggins C++ 0 07-09-2007 08:58 PM
SAX Parsing - Weird results when parsing content between tags. Naren XML 0 05-11-2004 07:25 PM
Perl expression for parsing CSV (ignoring parsing commas when in double quotes) GIMME Perl 2 02-11-2004 05:40 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57