I'm trying to parse a simple string in the format "YYYYww" (e.g. 201901) into a LocalDate, but none of my attempts succeed.
I attempted to parse it by simply using the pattern "YYYYww" and also through manually appending the values to the FormatterBuilder. Since my input string does not contain a day, I also configured the formatter to default to Sunday.
Here's the code that fails for me, running Java 8 (IBM JRE 8.0.5.25).
public static void main(String[] args) {
formatter1(); // Unable to obtain LocalDate from TemporalAccessor
formatter2(); // Text '201901' could not be parsed at index 0
formatter3(); // Text '201901' could not be parsed at index 6
}
public static void formatter1() {
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendValue(WeekFields.ISO.weekBasedYear(), 4, 4, SignStyle.NEVER)
.appendValue(WeekFields.ISO.weekOfYear(), 2, 2, SignStyle.NEVER)
.parseDefaulting(WeekFields.ISO.dayOfWeek(), DayOfWeek.SUNDAY.getValue())
.toFormatter();
LocalDate.parse("201901", formatter);
}
public static void formatter2() {
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendPattern("YYYYww")
.parseDefaulting(WeekFields.ISO.dayOfWeek(), DayOfWeek.SUNDAY.getValue())
.toFormatter();
LocalDate.parse("201901", formatter);
}
public static void formatter3() {
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.parseLenient()
.appendPattern("YYYYww")
.parseDefaulting(WeekFields.ISO.dayOfWeek(), DayOfWeek.SUNDAY.getValue())
.toFormatter();
LocalDate.parse("201901", formatter);
}
As seen in the example code I get different error messages, with especially the first example confusing me, since the TemporalAccessor contains the week-based-year, the week of the year and the week-day, which should be enough to construct a LocalDate.
Exception in thread "main" java.time.format.DateTimeParseException: Text '201901' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {WeekOfYear[WeekFields[MONDAY,4]]=1, WeekBasedYear[WeekFields[MONDAY,4]]=2019, DayOfWeek=7},ISO of type java.time.format.Parsed
at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1931)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1866)
at java.time.LocalDate.parse(LocalDate.java:411)
at Main.formatter1(Main.java:22)
at Main.main(Main.java:10)
Caused by: java.time.DateTimeException: Unable to obtain LocalDate from TemporalAccessor: {WeekOfYear[WeekFields[MONDAY,4]]=1, WeekBasedYear[WeekFields[MONDAY,4]]=2019, DayOfWeek=7},ISO of type java.time.format.Parsed
at java.time.LocalDate.from(LocalDate.java:379)
at java.time.LocalDate$$Lambda$7.000000001061ED20.queryFrom(Unknown Source)
at java.time.format.Parsed.query(Parsed.java:237)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1862)
... 3 more
TemporalAccessor a = formatter.parse("...");
that way you get the in between step, and it's a bit easier to walk through why things happens as they do :) – Handbagformatter2
andformatter3
work flawlessly on my Java 11. To getformatter1
to work, just replaceWeekFields.ISO.weekOfYear()
withWeekFields.ISO.weekOfWeekBasedYear()
– Domesticityformatter2()
throwsjava.time.format.DateTimeParseException: Text '201901' could not be parsed at index 0
.formatter3()
instead throwsjava.time.format.DateTimeParseException: Text '201901' could not be parsed at index 6
. I find it very interesting and cannot explain immediately. (Adding your Java version in the question will allow me to retract my downvote.) – Domesticity