I want to transform this data: “Thu, 06 Sep 2012 16:15:00 +0200” in the format yyyy-MM-dd HH:mm
this is my code:
private static final SimpleDateFormat rssFormat = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z");
public static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = rssFormat.parse(pubDate);
date.setHours(date.getHours()+2); //this is for gmt difference
this.pubDate = dateFormat.format(date);
it seems ok…why does it throw this exception?
java.text.ParseException: Unparseable date: "Tue, 04 Sep 2012 16:45:00 +0200"
Works for me – but then, I’m in a locale where “Thu” is a valid day abbreviation, and “Sep” is a valid month abbreviation. Perhaps you aren’t? Assuming you know you will be getting US-English day/month names, you should specify that. Try this:
Note that you absolutely should not be calling
Date.setHoursandDate.getHourslike this. Those methods have both been deprecated for over 15 years.Instead, you should be formatting with a
SimpleDateFormatset into an appropriate time zone… or preferrably, you should use Joda Time in the first place, which is a much more pleasant date/time API.