java - 无法使用季度从 TemporalAccessor 获取 LocalDate

标签 java datetime java-8

我试图将一些日期字符串解析为日期值,但是,使用下面的代码,我得到了一个异常:

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/

相关文章:

java - Spring MVC 错误消息

java - 无法扩展运行时编译的类

php - mysql数据库的时区问题

Java8 : "Translate" old ForEach Method into Lambda/Stream

java - 如何编写受 JDK bug 影响的 Java 单元测试

java - 如何过滤动态嵌套的列表对象java 8

Java向后兼容性说明

java - 将数据sql重命名为xml

python - 引用给定开始日期时间和结束日期时间的月份中某一天的每次出现

python - 使用字符串中的日期时间创建 Django 模型