migueloop
migueloop

Reputation: 541

Java DATE Parsing

I´m having a stupid problem with java.util.Date.

I have this line of code, but I don´t understand why this date is unparseable with this format.

public class TestTime {
    public static void main(String[] args) {
        final String DATE_FORMAT = "EEE MMM dd HH:mm:ss zzz yyyy";

        String date = "Sat Dec 31 10:00:00 CET 2011";
        SimpleDateFormat dFormat = new SimpleDateFormat(DATE_FORMAT);

        Date lDate = null;
        try {
            lDate = dFormat.parse(date);
        } catch (ParseException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
}

Upvotes: 4

Views: 563

Answers (3)

Olivier Croisier
Olivier Croisier

Reputation: 6149

Seems to be a Locale-related problem.

If I set a French locale, the pattern does not work. If I set the Locale to be US in the SimpleDateFormat constructor, it does works.

SimpleDateFormat dFormat = new SimpleDateFormat(DATE_FORMAT, Locale.US);

Upvotes: 1

stacker
stacker

Reputation: 69002

If your system uses a locale other than English you need to use this constructor:

SimpleDateFormat(DATE_FORMAT,Locale.ENGLISH);

If this is not the problem, you should format a date using the same formatter and compare the output to your input string.

Upvotes: 10

ziesemer
ziesemer

Reputation: 28707

I don't see anything wrong with this. It executes for me without error, and returns:

Sat Dec 31 09:00:00 GMT 2011

Upvotes: 5

Related Questions