javaandroidsimpledateformatdatetime-parsingparseexception

SimpleDateFormat Parsing on Android


I am trying to parse the following date/time:

Wed, 29 Aug 2018 12:56:00 +0200

Currently, I am using the following code which works on my Android emulator but on my actual phone I get a "java.text.ParseException: Unparseable date"

SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
this.pubDate = sdf.parse(s_pubDate);

Solution

  • You need to explicitly set the formatter locale, otherwise it would try to pars it based on phone locale and that may cause an error. I think that the cause in your case.

    Your emulated device has one locale, while your phone has the one, that can't pars date in such format.

    SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z",
                        **YOUR DESIRED LOCALE**);