Reputation: 36103
I use DateTimeFormatter to format a date. This works but when I try to convert this formatted date back to a date I get an exception.
Code:
DateTimeFormatter weekOfYear = DateTimeFormatter.ofPattern("w/yyyy");
LocalDate localDate = LocalDate.now();
String dateString = weekOfYear.format(localDate);
localDate = LocalDate.parse(dateString, weekOfYear);
Exception when I try to parse it:
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
Upvotes: 2
Views: 135
Reputation: 36103
The date cannot be parsed because the day part was missing.
So I had to set the default for the dayOfWeek:
DateTimeFormatter weekOfYear = new DateTimeFormatterBuilder().appendPattern("w/YYYY")
.parseDefaulting(WeekFields.ISO.dayOfWeek(), 1).toFormatter();
Upvotes: 6
Reputation: 157
Sorry but it's an expected behaviour. We couldn't guess an exact date based on the week number and year.
You could specify what are you trying to do so anyone could help.
Upvotes: 1