1

Have String str "May 23 2011 12:20:00", want to convert it to date such this:

Date date = (new SimpleDateFormat("MMM dd yyyy HH:mm:ss")).parse(str);

It always gives me ParseException Unparsable date format: 'May 23 2011 12:20:00'.

Looked for similar issues, seems everything right.

What is wrong?

pugmarx
  • 7,323
  • 3
  • 30
  • 40
Rinat Tainov
  • 1,479
  • 2
  • 19
  • 39
  • 2
    Works fine for me: `System.out.println((new SimpleDateFormat("MMM dd yyyy HH:mm:ss")).parse("May 23 2011 12:20:00"));` prints `Mon May 23 12:20:00 IST 2011` – Harry Joy May 26 '11 at 08:30
  • Perhaps you have an old version of Java. Which version are you using? – Peter Lawrey May 26 '11 at 08:32
  • Java version 1.6.0 update 21, may be import is wrong? is it from import java.text.SimpleDateFormat and java.text.ParseException? date from java.util – Rinat Tainov May 26 '11 at 08:39

5 Answers5

6

You may need to additionally specify the Locale, when the default Locale of your VM is not an English one:

Date date = (new SimpleDateFormat("MMM dd yyyy HH:mm:ss", Locale.US)).parse(str);
Andreas Krueger
  • 1,497
  • 13
  • 16
2

Works for me.!

jmj
  • 237,923
  • 42
  • 401
  • 438
1

tl;dr

LocalDateTime.parse(
    "May 23 2011 12:20:00" , 
    DateTimeFormatter.ofPattern( "MMM d uuuu HH:mm:ss" , Locale.US )
)

Using java.time

The accepted Answer by Andreas Krueger is correct. You must specify a Locale to determine the human language used in translating the name of month. Best to make a habit of always specifying a locale in date-time work.

The modern approach avoids the troublesome old date-time classes, instead using their replacements, the java.time classes.

String input = "May 23 2011 12:20:00" ;

Define a formatting pattern to match the input using the DateTimeFormatter class.

The Locale determines (a) the human language for translation of name of day, name of month, and such, and (b) the cultural norms deciding issues of abbreviation, capitalization, punctuation, separators, and such.

Locale locale = Locale.US ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "MMM d uuuu HH:mm:ss" , locale );

Your input lacks any indication of time zone or offset-from-UTC. So we parse as a LocalDateTime.

LocalDateTime ldt = LocalDateTime.parse( input , f ) ;

ldt.toString(): 2011-05-23T12:20:00


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

Try specifying locale while constructing SimpleDateFormat object.

user124
  • 1,632
  • 1
  • 11
  • 11
0

Probably It's a problem with a tool you use. Take a look at:

Why am I getting a ParseException when using SimpleDateFormat to format a date and then parse it?

Community
  • 1
  • 1
Oleg
  • 798
  • 3
  • 7