Java8+时间格式化错误

0

Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: HourOfDay
	at java.base/java.time.LocalDate.get0(LocalDate.java:708)
	at java.base/java.time.LocalDate.getLong(LocalDate.java:687)
	at java.base/java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:308)
	at java.base/java.time.format.DateTimeFormatterBuilder$NumberPrinterParser.format(DateTimeFormatterBuilder.java:2691)
	at java.base/java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2330)
	at java.base/java.time.format.DateTimeFormatter.formatTo(DateTimeFormatter.java:1843)
	at java.base/java.time.format.DateTimeFormatter.format(DateTimeFormatter.java:1817)
	at java.base/java.time.LocalDate.format(LocalDate.java:1816)
	at com.acgist.controller.APIController.main(APIController.java:25)

没有小时,这个问题是因为格式化的字符串里面包含小时,但是格式化的时间里面不包含:

System.out.println(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
System.out.println(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

上面两个代码,第一个行就会报上面的异常,第二个就正常了。