字符串转LocalDateTime出现异常:java.time.format.DateTimeParseException: Text '1900-01-01 00:00:00' could not be parsed at index 4
原代码:
DateTimeFormatter dfo = DateTimeFormatter.ofPattern("yyyy:MM:dd HH:mm:ss"); LocalDate localDate = LocalDate.parse("1900-01-01 00:00:00", dfo); LocalDateTime localDateTime = localDate.atStartOfDay(); System.out.println(localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
错误原因: 格式化的dfo引用不能放入LocalDate.parse("1900-01-01 00:00:00", dfo)中,要把格式化方法直接放入LocalDate.parse()中
修改代码 :
//DateTimeFormatter dfo = DateTimeFormatter.ofPattern("yyyy:MM:dd HH:mm:ss"); LocalDate localDate = LocalDate.parse("1900-01-01 00:00:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); LocalDateTime localDateTime = localDate.atStartOfDay(); System.out.println(localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)