SimpleDateFormat Unparseable date Error if locale

2019-07-23 12:15发布

I'm trying to convert the twitter "created_at" to an Argentinian Date-time. If I do this:

final String TWITTER="EEE MMM dd HH:mm:ss";
        SimpleDateFormat sf = new SimpleDateFormat(TWITTER,new Locale("en"));  

It works fine.

But if I change to Locale("es"), Locale("es","ES") or Locale("es","AR"), I am getting this error:

07-01 11:09:29.153: W/System.err(331): java.text.ParseException: Unparseable date: "Tue Jul 01 13:57:36 +0000 2014"

Why can't I convert the date to my local time?

EDIT:

Based on what Sotilios Delimanoris told me about using two SimpleDateFormat, one for parse and other for format, i've done this:

final String TWITTER="EEE MMM dd HH:mm:ss";  
SimpleDateFormat sf = new SimpleDateFormat(TWITTER,new Locale("en")); 
Date mystring = sf.parse(text); 
final String TWITTER2="dd-MM HH:mm:ss";  
SimpleDateFormat sf2 = new SimpleDateFormat(TWITTER2,new Locale("es","AR")); System.out.println(sf2.format(mystring)); 
return sf2.format(mystring); 

This is the output:

I/System.out(688): 01-07 18:41:31

Everything is ok, except for the hour, it's showing 2 hours more that what it should. –

I have tried setting time zone like this:

sf2.setTimeZone(TimeZone.getTimeZone("UTC-3"));

And now it's even worse, it shows 22:41:31

EDIT2:

Finally, it's working. Don't know why, but using:

sf2.setTimeZone(TimeZone.getTimeZone("GMT-6"));

Works fine (still in argentina is not GMT-6)

Thanks

1条回答
smile是对你的礼貌
2楼-- · 2019-07-23 12:39

You're trying to parse the date string in what I assume is Spanish, but Tue and Jul are not short words in Spanish. They are short for English words.

查看更多
登录 后发表回答