我试图将一些日期字符串解析为日期值,但是,使用下面的代码,我得到了一个异常:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("Q'Q'uuuu - MMM");
String d = "3Q2016 - Aug";
System.out.println(LocalDate.parse(d, formatter));
异常(exception)情况如下
java.time.format.DateTimeParseException: Text '3Q2016 - Aug' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {MonthOfYear=8, QuarterOfYear=3, Year=2016},ISO of type java.time.format.Parsed
查看异常,我看到了正确的数据,但由于某种原因无法解析。
我看到的其他类似主题建议使用 LocalDate 或 LocalDateTime,但都不起作用。
最佳答案
是因为指定的字符串没有具体的日期可以选择。您可能需要使用 YearMonth
而不是 LocalDateTime
,然后使用 YearMonth.atDay(1)
转换它以获得该月的第一天。
关于java - 无法使用季度从 TemporalAccessor 获取 LocalDate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55268992/