Reputation: 2653
My app is calling up a Twitter feed and a blog feed, both contain a post date of course. When my phone is set to English locale it works, when I switch to Dutch or German it fails. The code in question does not even call upon the locale, and the input values are also independent of the locale.
The offending code:
tweets is a JSONObject containing the complete Twitter feed.
final SimpleDateFormat formatter =
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
final SimpleDateFormat parser =
new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy");
for (int i = 0; i < tweets.length(); i++) {
final JSONObject tweet = tweets.getJSONObject(i);
// The following line is where the failure occurs:
values.put(KEY_TWEET_DATE, formatter.format(parser.parse(tweet
.getString("created_at"))));
}
This works as long as my locale is English.
As soon as I switch to German or Dutch (my app contains translations for those two languages, I haven't tried any other so far) I get an error like this:
WARN/System.err(28273): java.text.ParseException: Unparseable date: Wed Jun 29 10:55:41 +0000 2011
WARN/System.err(28273): at java.text.DateFormat.parse(DateFormat.java:645)
WARN/System.err(28273): at squirrel.DeaddropDroid.DeaddropDB.updateTwitter(DeaddropDB.java:1453)
The "unparseable date" is the correct date, in the expected format. My format string is designed to parse that exact date. And as said, when I switch my phone to English locale, it works just fine. It's the same code: the error occurs even when I switch the locale while the app is running, and disappears when I switch back the locale.
Upvotes: 1
Views: 2794
Reputation: 190
Have you tried:
SimpleDateFormat parser =
new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy",Locale.getDefault());
Upvotes: 1
Reputation: 1500913
If you need to parse in a particular locale, pass that into the SimpleDateFormat
constructor:
final SimpleDateFormat parser =
new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.US);
That means it will always use the US locale for day and month names etc.
Upvotes: 4