0

I have looked at many examples and can still not find an answer to match my problem. I have now been stuck for an hour and need help.

I have many strings that are formated like this:

Wed, 06 Nov 2013 18:14:02

I have created a function to convert these strings into Date:

private static Date toDate(String pubDateString) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss");

    pubDateString = pubDateString.substring(0, 25);
    Date date = null;
    try {
        date = dateFormat.parse(pubDateString);
    } catch (ParseException e1) {
        e1.printStackTrace();
    }

    return date;
}

What I get is a ParseException: java.text.ParseException: Unparseable date: "Wed, 06 Nov 2013 18:14:02"

Could anyone help me on my first challenge? Thanks.

edit : I tried HH, and kk

Big Al
  • 980
  • 1
  • 8
  • 20
Tumata
  • 1,507
  • 1
  • 11
  • 14

1 Answers1

1

(When I originally answered the question, the format string used hh - it has since been changed.)

You're using hh for the hours part, which is a 12-hour format - but providing a string which uses "18". You want HH.

Additionally, I'd suggest explicitly specifying the locale if you know that the values will always use English names.

I've verified that if you specify the locale explicitly, the code definitely works - at least under Oracle's Java 7 implementation:

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

If it wasn't working for you without the locale being specified (but with HH) that's probably why - presumably your system locale isn't English, so it was expecting different month and day names.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I forgot to change but I tried HH and kk too, thank you for such a fast answer though – Tumata Nov 07 '13 at 19:04
  • @Tumata: Changing your question to make it look like I'm talking nonsense isn't terribly polite. Did you *also* try specifying the locale, as I suggested? – Jon Skeet Nov 07 '13 at 20:05
  • I did not intend to make it look like this, my apologies about it. It did work after specifying the locale, the problem came from there. Thanks – Tumata Nov 21 '13 at 17:40