Unbeatable Date Exception

Well, I'm trying to catch a date from rss, and I get this execution in logcat:

E/AndroidNews( 870): Caused by: java.text.ParseException: Unparseable date: "Su n, 02 Oct 2011 14:00:00 +0100" E/AndroidNews( 870): at java.text.DateFormat.parse(DateFormat.java:626) E/AndroidNews( 870): at com.warriorpoint.androidxmlsimple.Message.setDate(Mes sage.java:57) 

My formatter is

static SimpleDateFormat FORMATTER = new SimpleDateFormat ("yyyy-MM-dd HH: mm"); ''

My setDate () method;

 public void setDate(String date) { this.date=null; // pad the date if necessary while (!date.endsWith("00")){ date += "0"; } try { this.date = FORMATTER.parse(date.trim()); } catch (ParseException e) { throw new RuntimeException(e); } } 
+4
source share
1 answer

You are using the wrong format, for "Sun, 02 Oct 2011 14:00:00 +0100" try this instead:

 new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.US) 

It also forces the locale to (American) English, so the name of the day and month is independent of the device locale, for example. waiting for "Dom" for "Sun" and "Oct" for "Out" in Portuguese (I looked at your profile).

The format that you have in your question will be able to handle dates like "2011-10-02 14:00".

Also, don't add a date; let your formatter do the parsing.


If you want to display / display a java.util.Date specific way, just create a new SimpleDateFormat (call it displayFmt ) with the desired format string and call format() on it:

 String formattedDate = new SimpleDateFormat("dd MM yyyy H").format(date); 

This will give you something like โ€œ02.10.2011 17โ€ today, formatted in the preferred user / device locale.

Please note that in your comment you said dd mm yyyy h (lowercase m and h ) which will give you โ€œdayOfMonth minutes year hours12โ€ - I think that is not what you want, so I changed it to uppercase m and uppercase h to give you "dayOfMonth month year hours24".

+9
source

All Articles