tags:

views:

79

answers:

4

Hello i need to parse this string

Sun, 15 Aug 2010 3:50 PM CEST

I'm using SimpleDataFormat in this way

String date = "Sun, 15 Aug 2010 3:50 pm CEST";
DateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy h:mm a Z");
Date d = formatter.parse(date);

but it throws an exception.

Can you help me please?

Thanks

+1  A: 

This code:

try {
    String date = "Sun, 15 Aug 2010 3:50 pm CEST";
    DateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy h:mm a Z");
    Date d = formatter.parse(date);
    System.out.println(formatter.format(d));
} catch (ParseException e) {
    e.printStackTrace();
}

Prints (without any exception):

Sun, 15 Aug 2010 3:50 PM +0200

So I guess something else is your problem... What is the exception you get?

draganstankovic
+4  A: 

SimpleDateFormat is sensitive to the Locale that is currently set. So it can be that there is a problem when trying to parse the format with your current one. With your constructor it uses Locale.getDefault() to determine the setting.

You could try to create the DateFormat explicitly using the Locale.US via new SimpleDateFormat(pattern, Locale.US) and verify if the problem also exists in that case.

Johannes Wachter
A: 

My code is

try {
    DateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy h:mm a Z", Locale.US);
    Date d = df.parse(date);
    bean.setDate(d);
}
catch (Exception e)
{
    Logger.error("Error while parsing data");
}

and exception is

java.text.ParseException: Unparseable date: Mon, 16 Aug 2010 2:20 pm CEST

Thanks

Premier
I cannot reproduce your problem. Works fine for me on JDK 1.5.0_14. And your input equals "Mon, 16 Aug 2010 2:20 pm CEST" without any additional whitespaces/characters?
Johannes Wachter
A: 

I've solved in this way

try {
DateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy h:mm a", Locale.US);
Date d = df.parse(date);
bean.setDate(d);

} catch (Exception e) { Logger.error("Error while parsing data"); }

Remove Z from pattern and use Locale.US.

Thanks

Premier