score:1

Accepted answer

week schemes differ (that’s the whole reason for having a weekfields class). you are using weekfields.iso for parsing and then weekfields.of(locale.getdefault()) for printing the individual fields back. that’s gotta give different results sometimes.

according to iso the first week of the year is the first week that contains at least 4 days of the new year. i can reproduce your week of 12 (+1 compared to the parsed week 11) in locales where week 1 is the week containing january 1. this is the case for example in some arabic locales (where the week begins on saturday) and in israel (where the week begins on sunday).

i believe that the solution is simple: use the same weekfields object for parsing and for printing (or equivalent weekfields objects).

score:0

new datetimeformatterbuilder()
        .appendvalue(chronofield.year, 4)
        .appendvalue(chronofield.aligned_week_of_year, 2)
        .appendvalue(chronofield.day_of_week, 1)
        .toformatter()

localdate.parse(datestring, customformatter).get(chronofield.day_of_week)


Related Query

More Query from same tag