4

I need to parse a string like "February 12, 1981" as a Date. I use SimpleDateFormat. But if I do:

new SimpleDateFormat("MMMMM dd, yyyy").parse("February 12, 1981")

I get java.text.ParseException.

I tried to reduce it to see where the problem is. First:

new SimpleDateFormat("MMMMM").parse("February")

works. Then:

new SimpleDateFormat("MMMMM dd").parse("February 12")

doesn't work anymore. Anyone know why? I also tried new SimpleDateFormat("MMMMM' 'dd").

I'm using JRE 1.6.0_06.

asterite
  • 7,761
  • 2
  • 23
  • 18

1 Answers1

7

What version of JDK/JRE are you using?

This works fine for me with 1.4.2_14, 1.5.0_16, and 1.6.0_07:

SimpleDateFormat df = new SimpleDateFormat("MMMMM dd, yyyy");
Date parsed = df.parse("February 12, 1981");
System.out.println(parsed);

output:

Thu Feb 12 00:00:00 EST 1981

matt b
  • 138,234
  • 66
  • 282
  • 345
  • Thanks! It turns out in 1.6.0_06 it doesn't work. I ask a friend who has 1.6.0_10 to try it and it worked. So it must have been a bug fixed in 1.6.0_07. :-) – asterite Oct 01 '08 at 18:19
  • I don't see it listed under the list of bugfixes in 1.6.0_07, but hey as long as it works... http://java.sun.com/javase/6/webnotes/ReleaseNotes.html#160_07 – matt b Oct 01 '08 at 18:24
  • Grrr... I just found out SimpleDateFormat parses localized dates, and my computer locale is, don't know why, Spanish, even though everything in the screen is in English. – asterite Oct 01 '08 at 18:35
  • You might want to update the original question to include the actual fix (your incorrect locale setting). – matt b Oct 02 '08 at 16:11