9

I can't understand why this few lines

    Date submissionT;
    SimpleDateFormat tempDate = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy");

    public time_print(String time) {
        try {
          submissionT=tempDate.parse(time);
        }
        catch (Exception e) {     
          System.out.println(e.toString() + ", " + time);
        }

    }

Cause exceptions and print out

    java.text.ParseException: Unparseable date: "Tue Mar 31 06:09:00 CEST 2009", Tue Mar 31 06:09:00 CEST 2009

... while the "unparsable" time is compliant with the format string i've passed to SimpleDateFormat().. Any Idea?

1
  • I've printed out with println().. time is a String and contains "Tue Mar 31 06:09:00 CEST 2009 " Commented Apr 23, 2009 at 11:12

3 Answers 3

24

It is a Locale issue. Use:

sdf = SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy", Locale.US);
Sign up to request clarification or add additional context in comments.

1 Comment

Would be nice if the exception message was not just "Unparseable date" but also the position in the String where it failed...
-1

Works for me.

public class Main {

public static void main(String[] args)
{
    time_print("Tue Mar 31 06:09:00 CEST 2009");
}

static Date submissionT;
static SimpleDateFormat tempDate = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy");

public static void time_print(String time) {
    try {
      submissionT=tempDate.parse(time);
      System.out.println(submissionT);
    }
    catch (Exception e) {
      System.out.println(e.toString() + ", " + time);
    }

}

}

1 Comment

It only works because of your default Locale matches the expected one.
-1

The 'z' in your format represents TimeZone and Java only recognises certain timezone ID's. You can get the list out of the TimeZone class as a String Array. CEST does not appear in the list I just generated from JDK 1.5

String[] aZones = TimeZone.getAvailableIDs();
    for (int i = 0; i < aZones.length; i++) {
        String string = aZones[i];
        System.out.println(string);
    }

Hope this helps.

Comments

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.