import java.util.Locale;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.format.ResolverStyle;
import java.time.temporal.WeekFields;

class Rextester
{
    public static void main(String args[])
    {
// ISO weeks locale
        Locale iso = Locale.forLanguageTag("it-CH");
        testAndPrint(iso, "2022.53.1");

// Non-ISO weeks locale
        Locale nonIso = Locale.forLanguageTag("en-SL");
        testAndPrint(nonIso, "2022.53.1");

// Another non-ISO weeks locale
        Locale anotherNonIso = Locale.forLanguageTag("teo-KE");
        testAndPrint(anotherNonIso, "2021.53.1");

        try {
// The following line gives the expected exception
// java.time.format.DateTimeParseException: Text '2021-W53-1' could not be parsed: Invalid value for WeekOfWeekBasedYear (valid values 1 - 52): 53
            LocalDate.parse("2021-W53-1", DateTimeFormatter.ISO_WEEK_DATE);
        } catch (DateTimeParseException dtpe) {
            System.out.println("As expected: " + dtpe);
        }
    }

    private static void testAndPrint(Locale localeInQuestion, String weekDateString) {
        DateTimeFormatter localizedWeekDateFormatter
                = DateTimeFormatter.ofPattern("YYYY.ww.e", localeInQuestion)
                .withResolverStyle(ResolverStyle.STRICT);
// We are expecting a DateTimeParseException from the following line but not getting any.
        LocalDate date = LocalDate.parse(weekDateString, localizedWeekDateFormatter);

        System.out.format("Locale: %s%n", localeInQuestion);
        System.out.format("Week fields: %s%n", WeekFields.of(localeInQuestion));
        System.out.format("String: %s%n", weekDateString);
        System.out.format("Parsed: %s%n", date);
        System.out.format("Formatted back: %s%n", date.format(localizedWeekDateFormatter));
        System.out.println();
    }

} 