无法在索引16处分析DateTimeParseException,Java很少触发

无法在索引16处分析DateTimeParseException,Java很少触发,java,datetime,Java,Datetime,最近我遇到了这个问题。但这种情况很少发生,有时我的程序运行平稳。触发此异常的是什么?如何彻底解决这个问题: 代码: 例外文本: Exception in thread "Thread-5" java.time.format.DateTimeParseException: Text '2015-11-21T14:15' could not be parsed at index 16 at java.time.format.DateTimeFormatter.parseResolved0(U

最近我遇到了这个问题。但这种情况很少发生,有时我的程序运行平稳。触发此异常的是什么?如何彻底解决这个问题:

代码:

例外文本:

Exception in thread "Thread-5" java.time.format.DateTimeParseException: Text '2015-11-21T14:15' could not be parsed at index 16
    at java.time.format.DateTimeFormatter.parseResolved0(Unknown Source)
    at java.time.format.DateTimeFormatter.parse(Unknown Source)
    at java.time.LocalDateTime.parse(Unknown Source)
    at com.fesca.view.MainMenuFrame.<init>(MainMenuFrame.java:65)
    at com.fesca.control.listener.CheckingRehabDateListener.run(CheckingRehabDateListener.java:287)
线程“thread-5”java.time.format.DateTimeParseException中的异常:无法在索引16处分析文本“2015-11-21T14:15” 位于java.time.format.DateTimeFormatter.parseResolved0(未知源) 位于java.time.format.DateTimeFormatter.parse(未知源) 位于java.time.LocalDateTime.parse(未知源) 在com.fesca.view.MainMenuFrame上。(MainMenuFrame.java:65) 位于com.fesca.control.listener.CheckingRehabDateListener.run(CheckingRehabDateListener.java:287) 线程“thread-5”中的异常 java.time.format.DateTimeParseException:文本“2015-11-21T14:15”可能 无法在索引16处解析

改变

yyyy-MM-dd'T'HH:mm:ss

因为您的格式设置最多为分钟(
2015-11-21T14:15
),所以不包括秒(
ss


或者添加秒部分。

您的日期字符串中有时不会出现ss,如生成日期时出错-
2015-11-21T14:15
所示。使用
yyyy-MM-dd'T'HH:MM
代替
yyy-MM-dd'HH:MM:ss

将格式化程序从yyy-MM-dd'T'HH:MM:ss更改为yyy-MM-dd'T'HH:MM,然后开始解析。您的输入时间缺少秒数。要么添加秒数,要么像乌斯曼说的那样更改格式化程序。@乌斯曼库尔德我的数据库中有秒数。当第二个值为00时出现问题。其他价值观似乎运作良好。有什么解决办法吗?我的数据库里有第二个。当第二个值为00时出现问题。其他价值观似乎运作良好。任何解决方案?如果秒数为00,请使用
yyyy-MM-dd'T'HH:MM:ss
@斯凯夫拉威尔
yyyy-MM-dd'T'HH:mm:ss
yyyy-MM-dd'T'HH:mm

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm", Locale.US);