LocalDateTime now = LocalDateTime.now();
final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd w hh:mm:ss");
final String format = now.format(dateTimeFormatter);
System.out.println(format);
final LocalDateTime parse = LocalDateTime.parse(format, dateTimeFormatter);
运行会出现
java.time.DateTimeException: Unable to obtain LocalTime from TemporalAccessor: {MilliOfSecond=0, NanoOfSecond=0, HourOfAmPm=9, MicroOfSecond=0, SecondOfMinute=57, MinuteOfHour=34},ISO resolved to 2020-04-13 of type java.time.format.Parsed
原因是因为时间格式中的小时 hh 采用12小时,反解析时不知道上午还是下午,改成 "yyyy-MM-dd w hh:mm:ss a"或者采用24小时制“yyyy-MM-dd w HH:mm:ss”
tips:年用YYYY格式也会出现异常
版权声明:本文为weixin_41450945原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。