gpt4 book ai didi

java - DateTimeFormatter 无法解析格式化日期

转载 作者:行者123 更新时间:2023-12-02 16:16:12 26 4
gpt4 key购买 nike

我使用 DateTimeFormatter 来格式化日期。这是可行的,但是当我尝试将此格式化日期转换回日期时,我遇到了异常。

代码:

DateTimeFormatter weekOfYear = DateTimeFormatter.ofPattern("w/yyyy");

LocalDate localDate = LocalDate.now();
String dateString = weekOfYear.format(localDate);

localDate = LocalDate.parse(dateString, weekOfYear);

当我尝试解析它时出现异常:

java.time.format.DateTimeParseException: Text '50/2019' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {Year=2019, WeekOfWeekBasedYear[WeekFields[MONDAY,4]]=50},ISO of type java.time.format.Parsed

at java.base/java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:2020)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1955)
at java.base/java.time.LocalDate.parse(LocalDate.java:428)
at DateTimeFormatterTest.weekOfYear(DateTimeFormatterTest.java:17)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
Caused by: java.time.DateTimeException: Unable to obtain LocalDate from TemporalAccessor: {Year=2019, WeekOfWeekBasedYear[WeekFields[MONDAY,4]]=50},ISO of type java.time.format.Parsed
at java.base/java.time.LocalDate.from(LocalDate.java:396)
at java.base/java.time.format.Parsed.query(Parsed.java:235)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1951)
... 24 more

最佳答案

无法解析日期,因为缺少日期部分。

所以我必须设置 dayOfWeek 的默认值:

DateTimeFormatter weekOfYear = new DateTimeFormatterBuilder().appendPattern("w/YYYY")
.parseDefaulting(WeekFields.ISO.dayOfWeek(), 1).toFormatter();

关于java - DateTimeFormatter 无法解析格式化日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59300841/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com