4

This question is a duplicate of this question by intention. It seems like the older one is "ignored", while none of the answers there is the answer.

I need to parse a given date with a given date pattern/format.

I have this code, which is supposed to work:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main
{

    public static void main(String[] args)
    {
        final Date date = string_to_date("EEE MMM dd HH:mm:ss zzz yyyy",
                "Thu Aug 14 16:45:37 UTC 2011");
        System.out.println(date);
    }

    private static Date string_to_date(final String date_format,
            final String textual_date)
    {
        Date ret = null;

        final SimpleDateFormat date_formatter = new SimpleDateFormat(
                date_format);
        try
        {
            ret = date_formatter.parse(textual_date);
        }
        catch (ParseException e)
        {
            e.printStackTrace();
        }

        return ret;
    }
}

For some reason I'm getting this output:

java.text.ParseException: Unparseable date: "Thu Aug 14 16:45:37 UTC 2011"
    at java.text.DateFormat.parse(DateFormat.java:337)
    at Main.string_to_date(Main.java:24)
    at Main.main(Main.java:10)
null

What's wrong with my date pattern? This seems to be a mystery.

5
  • 1
    Just look in the documentation of SimpleDateFormat Commented Aug 18, 2011 at 20:00
  • I think new Date('Thu Aug 14 16:45:37 UTC 2011'); actually works Commented Aug 18, 2011 at 20:02
  • @True Soft I did, several times, and still can't figure out the pattern I need. Why not write it down? I'd appriciate it. Commented Aug 18, 2011 at 20:25
  • Your code worked fine for me. I just copied and pasted and ran it. Commented Aug 18, 2011 at 22:11
  • 1
    Please DO NOT ask the same question again just because you aren't getting any useful responses to the original. Update the original question instead or offer a bounty. Commented Aug 18, 2011 at 23:08

3 Answers 3

11

Your platform default locale is apparently not English. Thu and Aug are English. You need to explicitly specify the Locale as 2nd argument in the constructor of SimpleDateFormat:

final SimpleDateFormat date_formatter = 
    new SimpleDateFormat(date_format, Locale.ENGLISH); // <--- Look, with locale.

Without it, the platform default locale will be used instead to parse day/month names. You can learn about your platform default locale by Locale#getDefault() the following way:

System.out.println(Locale.getDefault());
Sign up to request clarification or add additional context in comments.

2 Comments

Great answer. Never even occurred to me to think about the EEE and MMM
Man, you just rock! That was the issue. THANKS!
1

This should parse the given string.

public static void main(String[] args) throws ParseException
{
    String sd = "Thu Aug 14 16:45:37 UTC 2011";


    String dp = "EEE MMM dd HH:mm:ss zzz yyyy";

    SimpleDateFormat sdf = new SimpleDateFormat(dp);

    Date d = sdf.parse(sd);

    System.out.println(d);

}

2 Comments

Yes I tested, what error are you getting. Just be informed that the last sysout would print date in your OS timezone (not in UTC). You might have to use SimplateDateFormat for getting the date string in other than default timezone.
I get the 'ParseException'. That's very weird. And the code is so simple - no place for typo etc'..
0

I should have specify a locale, like this:

final SimpleDateFormat date_formatter = new SimpleDateFormat(date_format, Locale.ENGLISH);

Thanks to BalusC with his great answer!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.