Java – localdate cannot resolve ‘WW’ using ‘yyyy’

I must parse the date in the following format: "201710", in which the year number of 10 weeks I try to achieve it in this way:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyw");
java.time.LocalDate startDate = java.time.LocalDate.parse("201710",formatter);
System.out.println(startDate);

But it threw an exception:

java.time.format.DateTimeParseException: Text '201710' Could not be parsed at index 0

Then I need to get the first and last day of the week from the localdate object For example, "201710" – 05.03 12.03 (the first day of the week needs to be Sunday)

Solution

@Kayaman's accepted answer is incorrect because you cannot mix standard date representation (using yyyy = age) and Sunday representation (using WW = week of week based year) The subtle differences between standard calendar years and week - based years are relevant near the beginning or end of the calendar year Conclusion: do not use the symbol "Y", but use the symbol "Y" Counter example for entering "201501":

The right solution

DateTimeFormatter formatter =
    new DateTimeFormatterBuilder()
    .appendValue(WeekFields.ISO.weekBasedYear(),4)
    .appendValue(WeekFields.ISO.weekOfWeekBasedYear(),2)
    .parseDefaulting(ChronoField.DAY_OF_WEEK,1)
    .toFormatter();
LocalDate startDate = LocalDate.parse("201501",formatter);
System.out.println(startDate); // 2014-12-29

According to @ kayaman's proposal:

DateTimeFormatter dtf =
    new DateTimeFormatterBuilder()
    .appendValue(ChronoField.YEAR,4)
    .appendValue(ChronoField.ALIGNED_WEEK_OF_YEAR,2)
    .parseDefaulting(WeekFields.ISO.dayOfWeek(),1)
    .toFormatter();
System.out.println(LocalDate.parse("201501",dtf)); // 2015-01-05 (wrong)

The resulting date is different! The difference is caused by the definition of the calendar year, which always starts on January 1, while the week based year always starts on Monday (iso-8601 definition), using the first week of the calendar year with at least 4 days

Additional note a): java-8 does not manage the adjacent number resolution of definable fields, such as week based fields (see also the associated JDK issue), so I chose the Builder based solution, Instead of defining the pattern "yyyww" (Java, - 9 promises to provide a solution. However, even if java-9 is used, a build based method is still required, because the default value needs to be defined for the missing day of the week (here: set to Monday)

Additional note b): if you are looking for a real type for the week based year and anniversary combination and use localdate as a solution to this missing type, you can find this type in No. 3 - party library, whether in threeten extra or in my library time4j Example:

ChronoFormatter<CalendarWeek> cf =
        ChronoFormatter.ofPattern(
            "YYYYww",PatternType.CLDR,Locale.ROOT,CalendarWeek.chronology()
        );
    CalendarWeek cw = cf.parse("201501");
    System.out.println(cw); // 2015-W01
    System.out.println(cw.at(Weekday.MONDAY)); // 2014-12-29
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>