我有一个文本文件中的字符串这样的日子进行的注日期:String string = 2014-10-03 00:58:59.765
所以我分析这种方式:解析字符串与SimpleDateFormat的,毫秒改变
Date timestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")
.parse(string);
然而,一些时间戳较少在毫秒上超过3位,并且我得到一个ParseException,试图对它们使用相同的SimpleDateFormat。编辑:它发生在几个毫秒000例如:java.text.ParseException: Unparseable date: "2014-10-03 00:59:49"
这是在我的工作:
if (string.length() == 23) {
timestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")
.parse(string);
} else if (string.length() == 22) {
timestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SS")
.parse(string);
} else if string.length() == 21) {
timestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SS")
.parse(string);
} else if (string.length() == 19) {
timestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.parse(string);
}
是不是有更好的办法来做到这一点?
2014-10-03 00:58:59.7 2014-10-03 00:58:59.71 2014-10-03 00:58:59.716应该用yyyy-MM-dd HH:mm:ss.SSS 。你能粘贴你的字符串与你解析异常? – SMA 2014-10-22 11:35:51
是的,它在这种情况下毫秒为000:java.text.ParseException:Unparseable date:“2014-10-03 00:59:49” – George 2014-10-22 11:37:18