我想解析包含月份(1-12)和年份的日期,例如:
1.2015
12.2015
进入LocalDate
我使用此代码获得异常:
final DateTimeFormatter monthYearFormatter = DateTimeFormatter.ofPattern("M.yyyy");
LocalDate monthYearDate = LocalDate.parse(topPerformanceDate, monthYearFormatter);
java.time.format.DateTimeParseException:无法解析文本’6.2015’:无法从TemporalAccessor获取LocalDate:{MonthOfYear = 6,Year = 2015},ISO类型为java.time.format.Parsed的ISO
在短月份格式中,documentation对我来说并不清楚.
编辑:我想问题是每月缺少的一天?
解决方法:
由于您的输入不是日期而是月/年组合,我建议使用YearMonth类:
String input = "1.2015";
YearMonth ym = YearMonth.parse(input, DateTimeFormatter.ofPattern("M.yyyy"));
在评论中,您添加了需要该月的第一天和最后一天:
LocalDate firstOfMonth = ym.atDay(1);
LocalDate endOfMonth = ym.atEndOfMonth();
标签:java,datetime,java-8
来源: https://codeday.me/bug/20190715/1466275.html