我使用Java 8 New Date变量“ LocalDateTime”在方法中转换字符串:
@Override
public Object getAsDate(String value) {
DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE;
return LocalDateTime.parse(value);
}
当我用我的值调用此方法时,出现“无法解析”错误:
java.time.format.DateTimeParseException: Text '2018-10-20 18-20-03' could not be parsed at index 10
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.LocalDateTime.parse(LocalDateTime.java:492)
at java.time.LocalDateTime.parse(LocalDateTime.java:477)
我不知道我是否错过了什么。
答案 0 :(得分:2)
尝试:
@Override
public Object getAsDate(String value) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH-mm-ss");
return formatter.parse(value);
}