73

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

fospathi
  • 537
  • 1
  • 6
  • 7
tommy chheng
  • 9,108
  • 9
  • 55
  • 72
  • 3
    You have two spaces in your date string. – Anon. Feb 08 '10 at 01:37
  • Are you sure you're using Java 6? Those two lines of code you pasted is working fine for me; no exceptions. – aberrant80 Feb 08 '10 at 01:39
  • For late-comers to this question I recommend you don’t use `DateFormat`, `SimpleDateFormat` and `Date`. Those classes are notoriously troublesome and long outdated. Instead use `LocalDate` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Feb 13 '21 at 09:09

4 Answers4

155

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • so does the extra space have some influence in the parse error? – Humphrey Bogart Feb 08 '10 at 02:40
  • @Beau Martínez: No, that has no effect whatsoever. I've updated my comment to address that point since it has been brought up not just by you, but by others in the comments too. – Mark Byers Feb 08 '10 at 02:49
  • DateFormat fmt = new SimpleDateFormat("MMMM dd, yyyy", Locale.US); Date d = fmt.parse("Sat, June 27"); In my case its saying: java.text.ParseException: Unparseable date: "Sat, May 12" – Debasish Ghosh May 12 '18 at 12:30
12

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

Link

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

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
8

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.

gkephorus
  • 1,232
  • 18
  • 31
  • 1
    Thanks for showing the recommended, modern way (+1). A detail, the `TemporalAccessor` interface is considered low-level and is not for everyday use. It’s better to parse directly into a `LocalDate` as shown for example in my answer. Also you are not addressing the locale problem behind the question. – Ole V.V. Feb 13 '21 at 09:22
  • I code this error when running your exact code : Exception in thread "main" java.time.format.DateTimeParseException: Text 'June 27, 2007' could not be parsed at index 0 – bloub Oct 13 '21 at 16:08
  • I expect this to be due to the Locale you use. The code is expecting a Locale that does not know 'June' to be a month-name. (Same as with Mark Byers answer but than for Java 8 ;-) ) – gkephorus Oct 14 '21 at 07:30
-3
val currentTime = Calendar.getInstance().time
SimpleDateFormat("MMMM", Locale.getDefault()).format(date.time)
Aresan
  • 25
  • 6
  • 5
    This doesn't appear to answer the question. You are showing how to format (output) a date/time, and the OP asked how to parse (input) a date/time. – Wonko the Sane Jan 13 '20 at 20:02