问题描述
failure Text '1/1/2020 12:12:12' Could not be parsed at index 0
1/1/2020 12:12:12 MM/dd/yyyy HH:mm:ss
try {
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss",Locale.US)
.withResolverStyle(ResolverStyle.STRICT);
System.out.println("success " + dateFormatter.parse(1/1/2020 12:12:12));
} catch (Exception e) {
System.out.println("failure " + e.getMessage());
}
解决方法
DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss",Locale.US)
在月和日的两个日期中精确地 接受日期。输入DateTimeFormatter.ofPattern("M/d/yyyy HH:mm:ss",Locale.US),ResolverStyle.STRICT)
是必需的。