java.text.ParseException: Unparseable date: yyyy-MM-dd HH:mm:ss.SSSSSS

前端 未结 5 891
情话喂你
情话喂你 2020-12-19 06:57

I am getting ParseException for the following code

    String dateStr = \"2011-12-22 10:56:24.389362\";
    String formatStr = \"yyyy-MM-dd HH:m         


        
5条回答
  •  再見小時候
    2020-12-19 07:40

    The S format specifier refers to milliseconds. When you allow lenient parsing, the last part is interpreted as 389362 milliseconds. When that's added to the date so far, the last 3 digits (actually, the value % 1000) become the actual milliseconds, and you wind up with a date about 389 seconds (~6 1/2 minutes) later than you're expecting. (With strict parsing, the parser knows that 389362 milliseconds doesn't make sense, so it throws an error.)

    The simplest way around that, if you can guarantee the date will always look like that, would be to chop the last 3 digits off. (This will about half the time give you a date that's off by a millisecond. But that's better than having to write a date parser...)

提交回复
热议问题