SimpleDateFormat: exceptional date exception

After looking at a few existing messages, I still can't get the SimpleDateFormat parser to work. 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 get a ParseException. Here is the output of the debug messages:

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

Locale ist is installed and the template looks fine. Where am I mistaken?

+7
source share
2 answers

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

An 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 these characters with Notepad ++ in xml where the string is. However, obviously, there is something, and it works now.

Thanks for the help!

+4
source

Check your input for non-printable characters such as a tab (instead of a space), etc. Sometimes the reason he cannot make out has little to do with formatting numbers and a lot to do with unexpected characters (which you cannot always see).

Given that some people have already said β€œworks for me” (@Thanks Bozho!), I would strongly suspect non-printable characters in your input line. Who knows, you may have a vertical tab built in there!

+3
source

All Articles