LocalDate中yyyy-MM问题
需要实现个功能,展示包含当前所在月的近三个月数据,也就是当前输入2021-01数据为从2020-11~2021-01的;
写的是如下代码但是报错:
java.time.format.DateTimeParseException: Text '2020-01' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {Year=2020, MonthOfYear=1},ISO of type java.time.format.Parsed
1 LocalDate parse = LocalDate.parse("2020-01", DateTimeFormatter.ofPattern("yyyy-MM")); 2 String start_yyyy_MM = parse.plusMonths(-3).format(DateTimeFormatter.ofPattern("yyyy-MM")); 3 System.out.println(start_yyyy_MM);
后面向百度编程了:
1 YearMonth yearMonth = YearMonth.parse("2021-01", DateTimeFormatter.ofPattern("yyyy-MM")); 2 String start_yyyy_MM = yearMonth.plusMonths(-3).format(DateTimeFormatter.ofPattern("yyyy-MM")); 3 System.out.println(start_yyyy_MM);
输出的是2020-10,与我想要的2020-11有点差距,问题不大,查数据时再处理即可
搞定!
参考:https://blog.csdn.net/qq_37155440/article/details/105750593