在Mac OS X(Mavericks)上的Java 8(b132)的第一个发行版中,使用新的java.time包的此代码有效:String input = "20111203123456"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "yyyyMMddHHmmss");LocalDateTime localDateTime = LocalDateTime.parse( input, formatter );渲染:2011-12-03T12:34:56但是,当我在DateTimeFormatter类doc中指定的几分之一秒内添加“ SS”(并输入“ 55”)时,将引发异常:java.time.format.DateTimeParseException: Text '2011120312345655' could not be parsed at index 0该文档说,默认情况下使用严格模式,并且要求与输入数字位数相同的格式字符。所以我很困惑为什么这段代码失败:String input = "2011120312345655"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "yyyyMMddHHmmssSS");LocalDateTime localDateTime = LocalDateTime.parse( input, formatter );使用文档(“ 978”)中的示例的另一个示例(失败):String input = "20111203123456978"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "yyyyMMddHHmmssSSS");LocalDateTime localDateTime = LocalDateTime.parse( input, formatter );此示例有效,添加了一个小数点(但我在文档中没有找到这样的要求):String input = "20111203123456.978"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "yyyyMMddHHmmss.SSS");LocalDateTime localDateTime = LocalDateTime.parse( input, formatter );渲染:localDateTime: 2011-12-03T12:34:56.978从输入字符串或格式中省略句点字符会导致失败。失败:String input = "20111203123456.978"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "yyyyMMddHHmmssSSS");LocalDateTime localDateTime = LocalDateTime.parse( input, formatter );失败:String input = "20111203123456978"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "yyyyMMddHHmmss.SSS");LocalDateTime localDateTime = LocalDateTime.parse( input, formatter );
3 回答
慕工程0101907
TA贡献1887条经验 获得超5个赞
DateTimeFormatterBuilder#appendFraction(ChronoField.MILLI_OF_SECOND, 0, 3, true)
这样的事情帮助了我
添加回答
举报
0/150
提交
取消