0

I got a problem when I want to parse a String to a Date.

The String looks like this 2010-11-04 00:03:50.209589. But the result is Thu Nov 04 00:07:19 CET 2010 where the minutes and seconds are not correct.

String time_input = "2010-11-04 00:03:50.209589";
SimpleDateFormat  time_now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");

Date now = time_now.parse(time_input);

Thank you in advance for your help.

mouhannad
  • 39
  • 8
  • I believe this has been answered here http://stackoverflow.com/questions/5636491/date-object-simpledateformat-not-parsing-timestamp-string-correctly-in-java-and – Stanton Aug 18 '15 at 01:06

1 Answers1

3

You're using SSSSSS in your format string. S means milliseconds, so the formatter interprets 209589 as 209,589 milliseconds (3 minutes, 29 seconds, and 589 milliseconds). Add that to 00:03:50 and you'll end up with 00:07:19.

I don't think there is a way to include microsecond precision with DateFormat; you can try using JodaTime or classes from the java.time.format package if you have Java 8.

TNT
  • 2,900
  • 3
  • 23
  • 34
  • Thank you for the answer. I have removed the last 3 characters from the String in order to use `sss` format and it is working correctly. – mouhannad Aug 18 '15 at 09:11