Trying to format date and put it into TextView in Android Studio from
Mon, 29 Feb 2016 22:31:00 GMT
to
2016-02-29 22:31:00
In my Genymotion Google Nexus 5, Samsung Galaxy S5, etc devices it looks perfect. When i run this application on a real phone(not just one) TextView with date was empty: getFormatPubDate throws ParseException with "Unparseable date" on
date = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z").parse(pubDate);
Date in pubDate variable is correct and not null.
// strings.xml
<string name="data_time_format">yyyy-MM-dd HH:mm:ss</string>
// someClass.java
public String getFormatPubDate(String format){
try {
Date date = null;
date = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z").parse(pubDate);
return new SimpleDateFormat(format).format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return pubDate;
}
// someActivity.java
viewHolder.timePublishedTextView.setText(cachedNews.getFormatPubDate(activity.getResources().getString(R.string.data_time_format)));
P.S. I use format with HH (24 hour) but time is not correct. Example from emulator - Mon, 29 Feb 2016 23:01:00 GMT formated to 2016-02-29 18:01:00
//This is original date format
String date = "Mon, 29 Feb 2016 22:31:00";
SimpleDateFormat sdf = new SimpleDateFormat("EE, dd MMM yyyy HH:mm:ss",
Locale.ENGLISH);
//This is new date format
SimpleDateFormat print = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parsedDate = null;
try
{
parsedDate = sdf.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
String text = print.format(parsedDate);