How to parse month full form string using DateFormat in Java?

JavaSimpledateformat

Java Problem Overview


I tried this:

DateFormat fmt = new SimpleDateFormat("MMMM dd, yyyy");
Date d = fmt.parse("June 27, 2007");

error:

Exception in thread "main" java.text.ParseException: Unparseable date: "June 27, 2007"

The java docs say I should use four characters to match the full form. I'm only able to use MMM successfully with abbreviated months like "Jun" but i need to match full form.

> Text: For formatting, if the number > of pattern letters is 4 or more, the > full form is used; otherwise a short > or abbreviated form is used if > available. For parsing, both forms are > accepted, independent of the number of > pattern letters.

<https://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html>

Java Solutions


Solution 1 - Java

You are probably using a locale where the month names are not "January", "February", etc. but some other words in your local language.

Try specifying the locale you wish to use, for example Locale.US:

DateFormat fmt = new SimpleDateFormat("MMMM dd, yyyy", Locale.US);
Date d = fmt.parse("June 27,  2007");

Also, you have an extra space in the date string, but actually this has no effect on the result. It works either way.

Solution 2 - Java

Just to top this up to the new Java 8 API:

DateTimeFormatter formatter = new DateTimeFormatterBuilder().appendPattern("MMMM dd, yyyy").toFormatter();
TemporalAccessor ta = formatter.parse("June 27, 2007");
Instant instant = LocalDate.from(ta).atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
Date d = Date.from(instant);
assertThat(d.getYear(), is(107));
assertThat(d.getMonth(), is(5));

A bit more verbose but you also see that the methods of Date used are deprecated ;-) Time to move on.

Solution 3 - Java

LocalDate from java.time

Use LocalDate from java.time, the modern Java date and time API, for a date

	DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("MMMM d, u", Locale.ENGLISH);
	LocalDate date = LocalDate.parse("June 27, 2007", dateFormatter);
	System.out.println(date);

Output:

> 2007-06-27

As others have said already, remember to specify an English-speaking locale when your string is in English. A LocalDate is a date without time of day, so a lot better suitable for the date from your string than the old Date class. Despite its name a Date does not represent a date but a point in time that falls on at least two different dates in different time zones of the world.

Only if you need an old-fashioned Date for an API that you cannot afford to upgrade to java.time just now, convert like this:

	Instant startOfDay = date.atStartOfDay(ZoneId.systemDefault()).toInstant();
	Date oldfashionedDate = Date.from(startOfDay);
	System.out.println(oldfashionedDate);

Output in my time zone:

> Wed Jun 27 00:00:00 CEST 2007

Oracle tutorial: Date Time explaining how to use java.time.

Solution 4 - Java

val currentTime = Calendar.getInstance().time
SimpleDateFormat("MMMM", Locale.getDefault()).format(date.time)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiontommy chhengView Question on Stackoverflow
Solution 1 - JavaMark ByersView Answer on Stackoverflow
Solution 2 - JavagkephorusView Answer on Stackoverflow
Solution 3 - JavaOle V.V.View Answer on Stackoverflow
Solution 4 - JavaAresanView Answer on Stackoverflow