我必须在Java中使用自定义日期格式.它包含微秒,尽管Java不支持微秒.因为我用零填充时间模式,格式化时工作正常,但我不能用该模式解析日期字符串.
有一个简单的解决方法或我必须自己处理微秒(使用字符串函数)?
@Test
public void testDateFormat() throws ParseException {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss.SSS000");
String theDate = format.format(new Date());
// this will fail:
format.parse(theDate);
}
Run Code Online (Sandbox Code Playgroud)
java.text.ParseException:无法解析的日期:"2010-01-25-12.40.35.769000"