Evgeniy S
Evgeniy S

Reputation: 1484

Can't with String to Date convert

I have string looks like: Sun, 27-Oct-2013 11:31:31 GMT for make it a little simpler i do:

if (s.length() > 10)
   s = s.substring(5, s.length() - 4);

after that

s = " 27-Oct-2013 11:31:31"

SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
Date timestamp = null;

 try {
   timestamp = formatter.parse(s.trim());
 } catch (ParseException e) {
   e.printStackTrace();
 }

It catch exception java.text.ParseException: Unparseable date: "27-Oct-2013 11:31:31"

Upvotes: 1

Views: 86

Answers (1)

Alexis C.
Alexis C.

Reputation: 93842

You should specify a locale where months are spelled in english for your formatter, otherwise it will use your default locale.

SimpleDateFormat(String pattern)

Constructs a newSimpleDateFormatusing the specified non-localized pattern and theDateFormatSymbolsandCalendarfor the user's default locale.

SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss", Locale.US);

Upvotes: 7

Related Questions