Constantin Treiber
Constantin Treiber

Reputation: 420

SimpleDateFormat can't parse Date

i've got a little problem with parsing a String to date. I searched on stackoverflow an thought that i got my answer but its still not working .. so ..

I trying to parse a String which looks like this

Tue Jan 08 00:00:00 CET 1985

what I'm trying ist this..

private Date getDateFromString(String sDate)
{
    String dateFormat = "EEE MMM dd HH:mm:ss z yyyy";

    SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
    sdf.setTimeZone(TimeZone.getTimeZone("CET"));  
    Date newDate = null;
    try {
        newDate = sdf.parse(sDate);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return newDate;

}

Sure somebody can help me :-) Thank you in forcast

Upvotes: 3

Views: 4093

Answers (2)

Oxenarf
Oxenarf

Reputation: 278

Have you tried whit Calendar?

public Date getDateFromString(String sdate) {
    SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
    Calendar car = Calendar.getInstance();
    Date ndate = null;
    try {
        ndate = sdf.parse(sdate);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return ndate;
}

Upvotes: 0

Arnaud Denoyelle
Arnaud Denoyelle

Reputation: 31215

The problem is on the interpretation on "Tue" and "Jan" because you did not specify any locale (in this case, it takes the default locale (Locale.getDefault())

Try:

new SimpleDateFormat(dateFormat, new Locale("en_US"));

Upvotes: 7

Related Questions