I have written a func to convert Date String (Thu, 1 Mar 2012 13:57:06 -0600) to Date variable.
I’m relying on SimpleDateFormat for the conversion. The conversion happens, but the resultant Date is “Thu Sep 01 11:00:06 GMT-08:00 2016” which I’m not able to understand. I tried different options for the Format string still invain. Any help appreciated. Thank you.
The options I have tried for the Format String are:
1) E, dd MMM yyyy HH:MM:ss Z
2) EEE, dd MMM yyyy HH:MM:ss ZZZZ
private Date Convert_To_Date() {
Date dt = null;
String str = "Thu, 1 Mar 2012 13:57:06 -0600";
SimpleDateFormat formatter = new SimpleDateFormat("E, dd MMM yyyy HH:MM:ss Z");
try {
dt = formatter.parse(str);
}
catch ( ParseException pe) {
System.out.println(pe.getMessage());
}
//String strDt = dt.toString();
System.out.println(dt);
return dt;
}
I think the problem was just that you were using “M” for minutes instead of “m”. This works fine for me:
(As noted by sgmorrison, you can use
dinstead ofddtoo, and that would be a more accurate description of your format – butddworks for parsing in this case.)