Well, i'm trying to catch date from rss, and i get this execption 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 method setDate();
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);
}
}
You're 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)
This also forces the locale to (American) English, so the day and month names aren't affected by the device's locale, e.g. expecting "Dom" for "Sun" and "Oct" for "Out" in a Portuguese locale (I peeked at your profile).
The format you have in your question would only be able to parse dates like "2011-10-02 14:00".
Also, don't pad the date, let your formatter do the parsing.
If you want to output/display a java.util.Date
in a certain way, just create a new SimpleDateFormat
(let's call it displayFmt
) with the desired formatting 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" for today, formatted in the user's/device's preferred locale.
Please note that in your comment, you said dd mm yyyy h
(lower case m
and h
) which would give you "dayOfMonth minutes year hours12" -- I think that's not what you want, so I changed it to an upper case M
and upper case H
to give you "dayOfMonth month year hours24".
精彩评论