Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected java SimpleDateFormat parse exception

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?

like image 747
Emilio Avatar asked Apr 23 '09 11:04

Emilio


People also ask

What does SimpleDateFormat parse do?

The parse() Method of SimpleDateFormat class is used to parse the text from a string to produce the Date. The method parses the text starting at the index given by a start position.

What is parse exception in Java?

Class ParseExceptionSignals that an error has been reached unexpectedly while parsing. See Also: Exception , Format , FieldPosition , Serialized Form.

Is SimpleDateFormat parse thread-safe?

Java's SimpleDateFormat is not thread-safe, Use carefully in multi-threaded environments. SimpleDateFormat is used to format and parse dates in Java. You can create an instance of SimpleDateFormat with a date-time pattern like yyyy-MM-dd HH:mm:ss , and then use that instance to format and parse dates to/from string.

Is SimpleDateFormat thread-safe in Java?

SimpleDateFormat is not thread-safe in any JDK version, nor will it be as Sun have closed the bug/RFE. Only formatting is supported, but all patterns are compatible with SimpleDateFormat (except time zones - see below).


1 Answers

It is a Locale issue. Use:

sdf = SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy", Locale.US);
like image 192
kgiannakakis Avatar answered Nov 02 '22 23:11

kgiannakakis