I have created this funtion to parse date but this gives exception : Unparseable date: "Fri Oct 10 23:11:07 IST 2014" (at offset 20) . Please help as I am not able to figure out whats wrong with this code.
public Date parseDate() {
String strDate ="Fri Oct 10 23:11:29 IST 2014";
String newPattern = "EEE MMM dd HH:mm:ss Z yyyy";
SimpleDateFormat formatter = new SimpleDateFormat(newPattern);
try {
Date date = formatter.parse(strDate);
return date;
} catch (java.text.ParseException e) {
e.printStackTrace();
}
return null;
}
Use a locale for the parser:
SimpleDateFormat formatter = new SimpleDateFormat(newPattern, Locale.US);
This should fix your problem. At least it works for me with your example.
EDIT:
It looks like there is indeed a problem with Android and IST timezone. I can parse any time zone on Android using the above pattern, but not IST.
A quick hack is to modify the timezone part, if there is an IST zone in the string. This works for me also on Android:
String strDate = "Fri Oct 10 23:11:29 IST 2014";
strDate = strDate.replace(" IST ", " GMT+0530 ");
String newPattern = "EEE MMM dd HH:mm:ss Z yyyy";
SimpleDateFormat formatter = new SimpleDateFormat(newPattern, Locale.ENGLISH);
SimpleDateFormat formatter = new SimpleDateFormat(newPattern);
formatter.setLenient(true);
//setting the formatter to be lenient solves the problem
Thanks folks for helping....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With