1

I have used SimpleDateFormat to parse Strings into Dates many times in the past, but today I ran across an error that I cannot seem to see. I am parsing a csv, and I have this:

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date date = dateFormat.parse(nextLine[0]);

I get this error

java.text.ParseException: Unparseable date: "2011-06-17 21:43:17.493"

It looks to me like the format matches the string when referencing the javadoc for SimpleDateFormat here: http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

am I missing something with SimpleDateFormat?

3
  • 1
    Stupid suggestion, but what if you trim the String? dateFormat.parse(nextLine[0].trim()); Commented Jul 28, 2013 at 2:30
  • Any way you can post a portion of your CSV, at least the offending portion? Commented Jul 28, 2013 at 2:36
  • 1
    What charset of CSV? [From doc: The format is locale independent and digits must be taken from the Basic Latin block of the Unicode standard.] Commented Jul 28, 2013 at 2:36

1 Answer 1

3

Nothing is worng with your code. You just have invisible symbols at this line

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

You probably copied it from a PDF file or other source that inject that symbols into your text.

Try to copy it manually and it will be fine. It work for me after I done so.

Sign up to request clarification or add additional context in comments.

1 Comment

Yep, I'll buy this. 1+

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.