SimpleDateFormat Unparseable date Error if locale is ES. Twitter “Created_At”

好久不见. 提交于 2019-12-11 03:45:12

问题


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:


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.



来源:https://stackoverflow.com/questions/24512751/simpledateformat-unparseable-date-error-if-locale-is-es-twitter-created-at

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!