0

I need to parse the string "October 2015" but I´m getting a java.text.ParseException. I've been looking for an answer but all the examples I've seen include the "day" within the string. I need to convert this string without specifying the day.

This is my code:

SimpleDateFormat format = new SimpleDateFormat("MMMM yyyy");

System.out.println("La fecha despues de convertida " + format.parse(search.getFecha()));

And this is the exception:

java.text.ParseException: Unparseable date: "October 2015"
Luis Teijon
  • 4,769
  • 7
  • 36
  • 57
Lorena Pita
  • 1,366
  • 1
  • 17
  • 20
  • 1
    What format did you use? `"MMMM yyyy"` seems to work okay for me – MadProgrammer Oct 09 '15 at 02:48
  • You can use StringTokenizer class with space character as delimiter to get month and year – MIK Oct 09 '15 at 02:53
  • I edited the question. – Lorena Pita Oct 09 '15 at 15:53
  • 1
    Have you at least tried to read the duplicate question and its best answer? I'll give you a hint: Since you're not from an english speaking country (according to your profile) your JVM presumably uses a `Locale` which doesn't understand *"October"* and has its own word for that month. So try to tell him to use a Locale which "knows" and understands this word. The duplicate question shows you how to do that. (repost to fix a nasty typo) – Tom Oct 09 '15 at 17:53

2 Answers2

2

All you have to do is to specify your date pattern,

DateFormat dateFormat = new SimpleDateFormat("MMMM yyyy");
dateFormat.parse("October 2014");
Ratshiḓaho Wayne
  • 743
  • 1
  • 5
  • 23
0

Use 'yyyy' instead of 'YYYY'

In Java 1.6 and prior, 'YYYY' was used to parse the year of the week and in further versions, it got discontinued.

EDIT: I mixed up the casing. Please ignore this answer as it is clearly not related to this problem at all.

javaguest
  • 399
  • 2
  • 8
  • 1
    Can you cite a reference for your claim about 'yyyy' having been used for week of the year? In [Java 6](http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html) and [Java 5](http://docs.oracle.com/javase/1.5.0/docs/api/java/text/SimpleDateFormat.html) I see `w` used of “Week in year”. – Basil Bourque Oct 09 '15 at 21:46
  • So in Java 1.6 `yyyy` was used for "year of the week" even if the source code suggests something else and in 1.7 `YYYY` replaced it, because this is now "year of the week"? interesting. – Tom Oct 09 '15 at 21:47
  • http://stackoverflow.com/questions/27739514/simpledateformatdd-mmm-yyyy-printing-year-one-year-ahead?lq=1 I mixed up the case, sorry. – javaguest Oct 13 '15 at 14:23