I have a date string in the following format:
Thu Oct 20 14:39:19 PST 2011
I would like to parse it using DateFormat to get a Date object. I'm trying it like this:
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT);
Date date = df.parse(dateString);
This gives a ParseException ("unparseable date").
I've also tried this:
SimpleDateFormat df = new SimpleDateFormat("EEE-MMM-dd HH:mm:ss z yyyy");
with the same results.
Is that the right SimpleDateFormat
string? Is there a better way to parse this date?
Are you sure that date string is really being assigned the value Thu Oct 20 14:39:19 PST 2011
? If that's not the problem, you could try using this code which works for me:
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test{
public static void main(String args[]){
String toParse = "Thu Oct 20 14:39:19 PST 2011";
String format = "EEE MMM dd HH:mm:ss z yyyy";
SimpleDateFormat formater = new SimpleDateFormat(format);
try{
Date parsed = formater.parse(toParse);
} catch(Exception e){
System.out.println(e.getMessage());
}
}
}
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