7

After looking after several existing posts, I am still not able to get my SimpleDateFormat parser working. Here is the code:

SimpleDateFormat df = new SimpleDateFormat(
    "EEE, dd MMM yyyy HH:mm:ss Z", Locale.US);
try {
    volcanoListDate = df.parse(currentValue);
} catch (ParseException e) {
    Log.d("DEBUG", e.toString());
    Log.d("DEBUG", currentValue);
}

I always end up with a ParseException. Here is the output of the debug messages:

06-09 23:52:17.478: DEBUG/DEBUG(2436): java.text.ParseException: Unparseable date:
06-09 23:52:17.478: DEBUG/DEBUG(2436): Wed, 08 Jun 2011 03:23:55 -0500

Locale ist set and the pattern looks okay. Where am I wrong?

3
  • 5
    Try Locale.ENGLISH. Perhaps Android doesn't support country specific locales.
    – BalusC
    Jun 9, 2011 at 22:24
  • Already tried this. It does not work with Locale.English, UK or without a Locale. Jun 9, 2011 at 22:51
  • @BalusC thanks for that fix; I had no second parameter at all, and some devices were failing, but others not. I assume it had to do with which version of Android they're running... Sep 29, 2011 at 5:09

2 Answers 2

4

Here is the solution:

            SimpleDateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.US);
        try {
            volcanoListDate = df.parse(currentValue.replaceAll("\\p{Cntrl}", ""));
        } catch (ParseException e) {
            Log.d("VOLCANO_DEBUG", e.toString());
            Log.d("VOLCANO_DEBUG", currentValue);
        }

The important change is .replaceAll("\\p{Cntrl}", "") which removes control characters from the parsed string. The strange thing is, that I do not see any of those characters with Notepad++ in the xml where the string is from. However, obviously there is something and it is working now.

Thanks for all the help!

3

Check your input for non-printing characters, like tab (instead of space), etc. Sometimes the reason it can't parse has little to do with the formatting of the numbers and a lot to do with unexpected characters (that you can't always see).

Considering some people have already reported "works for me" (@Thanks Bozho!) I would strongly suspect unprintable characters in your input string. Who knows, you might have a vertical tab embedded in there somewhere!

1
  • Thanks, works for me! The solution using .replaceAll("\\p{Cntrl}", "") is posted below. Previously I alreadey tried removing all whitespaces which did not help. Jun 10, 2011 at 5:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.