ke3pup
ke3pup

Reputation: 1895

Java String to Date Conversion throws Unparseable date

I'm trying to convert following text to Date but not able to get it to parse correctly.

String strDate = "Tue Mar 13 12:00:00 EST 2012";
try{
    SimpleDateFormat sd = new SimpleDateFormat("dd/MM/yyyy");
    //also tried SimpleDateFormat sd = new SimpleDateFormat("dd/MM/yyyy HH:mm");
    //as well as sd.setTimeZone(TimeZone.getDefault());
    Date date = sd.parse(strDate);
}catch(Exception e)
{
    e.printStack();
    //fails with java.text.ParseException: Unparseable date: "Tue Mar 13 12:00:00 EST 2012"
}

what am i doing wrong?

Upvotes: 1

Views: 3928

Answers (2)

DwB
DwB

Reputation: 38328

Try using a date format that matches the input.

DateFormat sd = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");

edit: fixed the date format string.

Note: HH is hour in day not am/pm hour.

Upvotes: 5

Jeff Storey
Jeff Storey

Reputation: 57212

Your date format is looking for something in the format dd/MM/yyyy (day/month/year), but you are passing in a long date string. Your format would parse a date like 13/05/2012. Look at the SimpleDateFormat http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html javadocs for the symbols.

So your date format would be something like EEE MMM dd...etc

Upvotes: 6

Related Questions