4

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();
        }
    }
}
4
  • 3
    Check your default locale, is it en_US, etc. or something 'obscure'? Commented Dec 9, 2011 at 16:28
  • Show the exception trace Commented Dec 9, 2011 at 16:31
  • 1
    Be careful using SimpleDateFormat. It is not thread safe. codefutures.com/weblog/andygrove/2007/10/… Commented Dec 9, 2011 at 16:31
  • What character is that in your "I'm" and "don't" (for apostrophe)? Commented Dec 9, 2011 at 16:47

3 Answers 3

10

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.

Sign up to request clarification or add additional context in comments.

Comments

5

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

Sat Dec 31 09:00:00 GMT 2011

2 Comments

Without a stack trace, it was a valid effort - and at a minimum, it shows that there is nothing that is "completely incorrect" with the code, and must be something environment-specific. +1 for the locale suggestions.
Thanks a lot!, It was a Locale Problem!!
1

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);

1 Comment

Thanks Olivier, it finally was a Locale problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.