kos*_*čák 3 java datetime java-8
我想解析包含月份(1-12)和年份的日期,例如:
1.2015
12.2015
Run Code Online (Sandbox Code Playgroud)
成 LocalDate
我使用此代码获得异常:
final DateTimeFormatter monthYearFormatter = DateTimeFormatter.ofPattern("M.yyyy");
LocalDate monthYearDate = LocalDate.parse(topPerformanceDate, monthYearFormatter);
Run Code Online (Sandbox Code Playgroud)
java.time.format.DateTimeParseException:无法解析文本'6.2015':无法从TemporalAccessor获取LocalDate:{MonthOfYear = 6,Year = 2015},ISO类型为java.time.format.Parsed的ISO
该文档北京时间不太清楚,我在短短的一个月的格式.
编辑:我想问题是每月缺少的一天?
由于您的输入不是日期而是月/年组合,我建议使用YearMonth
该类:
String input = "1.2015";
YearMonth ym = YearMonth.parse(input, DateTimeFormatter.ofPattern("M.yyyy"));
Run Code Online (Sandbox Code Playgroud)
在评论中,您添加了需要该月的第一天和最后一天:
LocalDate firstOfMonth = ym.atDay(1);
LocalDate endOfMonth = ym.atEndOfMonth();
Run Code Online (Sandbox Code Playgroud)