--- old/src/share/classes/java/time/temporal/WeekFields.java 2013-04-11 23:15:56.000000000 -0700 +++ new/src/share/classes/java/time/temporal/WeekFields.java 2013-04-11 23:15:55.000000000 -0700 @@ -68,6 +68,7 @@ import static java.time.temporal.ChronoField.MONTH_OF_YEAR; import static java.time.temporal.ChronoField.YEAR; import static java.time.temporal.ChronoUnit.DAYS; +import static java.time.temporal.ChronoUnit.FOREVER; import static java.time.temporal.ChronoUnit.MONTHS; import static java.time.temporal.ChronoUnit.WEEKS; import static java.time.temporal.ChronoUnit.YEARS; @@ -77,14 +78,18 @@ import java.time.DayOfWeek; import java.time.chrono.ChronoLocalDate; import java.time.chrono.Chronology; +import java.time.format.ResolverStyle; import java.util.Collections; import java.util.HashMap; import java.util.Locale; import java.util.Map; import java.util.Objects; +import java.util.ResourceBundle; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import sun.util.locale.provider.CalendarDataUtility; +import sun.util.locale.provider.LocaleProviderAdapter; +import sun.util.locale.provider.LocaleResources; /** * Localized definitions of the day-of-week, week-of-month and week-of-year fields. @@ -93,8 +98,9 @@ * other aspects of a week. This class represents the definition of the week, for the * purpose of providing {@link TemporalField} instances. *

- * WeekFields provides three fields, - * {@link #dayOfWeek()}, {@link #weekOfMonth()}, and {@link #weekOfYear()} + * WeekFields provides five fields, + * {@link #dayOfWeek()}, {@link #weekOfMonth()}, {@link #weekOfYear()}, + * {@link #weekOfWeekBasedYear()}, and {@link #weekBasedYear()} * that provide access to the values from any {@linkplain Temporal temporal object}. *

* The computations for day-of-week, week-of-month, and week-of-year are based @@ -110,7 +116,7 @@ *

  • The first day-of-week. * For example, the ISO-8601 standard considers Monday to be the first day-of-week. *
  • The minimal number of days in the first week. - * For example, the ISO-08601 standard counts the first week as needing at least 4 days. + * For example, the ISO-8601 standard counts the first week as needing at least 4 days. *

    * Together these two values allow a year or month to be divided into weeks. *

    @@ -134,14 +140,37 @@ * 2009-01-05Monday * Week 2 of January 2009Week 1 of January 2009 * - *

    + * *

    Week of Year

    * One field is used: week-of-year. * The calculation ensures that weeks never overlap a year boundary. * The year is divided into periods where each period starts on the defined first day-of-week. * The earliest period is referred to as week 0 if it has less than the minimal number of days * and week 1 if it has at least the minimal number of days. - *

    + * + *

    Week Based Year

    + * Two fields are used for week-based-year, one for the + * {@link #weekOfWeekBasedYear() week-of-week-based-year} and one for + * {@link #weekBasedYear() week-based-year}. In a week-based-year, each week + * belongs to only a single year. Week 1 of a year is the first week that + * starts on the first day-of-week and has at least the minimum number of days. + * The first and last weeks of a year may contain days from the + * previous calendar year or next calendar year respectively. + * + * + * + * + * + * + * + * + * + * + * + * + * + *
    Examples of WeekFields for week-based-year
    DateDay-of-weekFirst day: Monday
    Minimal days: 4
    First day: Monday
    Minimal days: 5
    2008-12-31WednesdayWeek 1 of 2009Week 53 of 2008
    2009-01-01ThursdayWeek 1 of 2009Week 53 of 2008
    2009-01-04SundayWeek 1 of 2009Week 53 of 2008
    2009-01-05MondayWeek 2 of 2009Week 1 of 2009
    + *

    Specification for implementors

    * This class is immutable and thread-safe. * * @since 1.8 @@ -171,19 +200,41 @@ * Note that the first week may start in the previous calendar year. * Note also that the first few days of a calendar year may be in the * week-based-year corresponding to the previous calendar year. + *

    + * This field is an immutable and thread-safe singleton. */ public static final WeekFields ISO = new WeekFields(DayOfWeek.MONDAY, 4); /** - * The common definition of a week that starts on Sunday. + * The common definition of a week that starts on Sunday and the first week + * has a minimum of 1 day. *

    * Defined as starting on Sunday and with a minimum of 1 day in the month. * This week definition is in use in the US and other European countries. - * + *

    + * This field is an immutable and thread-safe singleton. */ public static final WeekFields SUNDAY_START = WeekFields.of(DayOfWeek.SUNDAY, 1); /** + * The unit that represents week-based-years for the purpose of addition and subtraction. + *

    + * This allows a number of week-based-years to be added to, or subtracted from, a date. + * The unit is equal to either 52 or 53 weeks. + * The estimated duration of a week-based-year is the same as that of a standard ISO + * year at {@code 365.2425 Days}. + *

    + * The rules for addition add the number of week-based-years to the existing value + * for the week-based-year field retaining the week-of-week-based-year + * and day-of-week, unless the week number it too large for the target year. + * In that case, the week is set to the last week of the year + * with the same day-of-week. + *

    + * This field is an immutable and thread-safe singleton. + */ + public static final TemporalUnit WEEK_BASED_YEARS = IsoFields.WEEK_BASED_YEARS; + + /** * Serialization version. */ private static final long serialVersionUID = -1177360819670808121L; @@ -213,6 +264,24 @@ private transient final TemporalField weekOfYear = ComputedDayOfField.ofWeekOfYearField(this); /** + * The field that represents the week-of-week-based-year. + *

    + * This field allows the week of the week-based-year value to be queried and set. + *

    + * This unit is an immutable and thread-safe singleton. + */ + private transient final TemporalField weekOfWeekBasedYear = ComputedDayOfField.ofWeekOfWeekBasedYearField(this); + + /** + * The field that represents the week-based-year. + *

    + * This field allows the week-based-year value to be queried and set. + *

    + * This unit is an immutable and thread-safe singleton. + */ + private transient final TemporalField weekBasedYear = ComputedDayOfField.ofWeekBasedYearField(this); + + /** * Obtains an instance of {@code WeekFields} appropriate for a locale. *

    * This will look up appropriate values from the provider of localization data. @@ -341,7 +410,7 @@ * Returns a field to access the week of month, * computed based on this WeekFields. *

    - * This represents concept of the count of weeks within the month where weeks + * This represents the concept of the count of weeks within the month where weeks * start on a fixed day-of-week, such as Monday. * This field is typically used with {@link WeekFields#dayOfWeek()}. *

    @@ -367,12 +436,12 @@ * Returns a field to access the week of year, * computed based on this WeekFields. *

    - * This represents concept of the count of weeks within the year where weeks + * This represents the concept of the count of weeks within the year where weeks * start on a fixed day-of-week, such as Monday. * This field is typically used with {@link WeekFields#dayOfWeek()}. *

    * Week one(1) is the week starting on the {@link WeekFields#getFirstDayOfWeek} - * where there are at least {@link WeekFields#getMinimalDaysInFirstWeek()} days in the month. + * where there are at least {@link WeekFields#getMinimalDaysInFirstWeek()} days in the year. * Thus, week one may start up to {@code minDays} days before the start of the year. * If the first week starts after the start of the year then the period before is week zero (0). *

    @@ -390,7 +459,59 @@ } /** - * Checks if these rules are equal to the specified rules. + * Returns a field to access the week of a week-based-year, + * computed based on this WeekFields. + *

    + * This represents the concept of the count of weeks within the year where weeks + * start on a fixed day-of-week, such as Monday and each week belongs to exactly one year. + * This field is typically used with {@link WeekFields#dayOfWeek()} and + * {@link WeekFields#weekBasedYear()}. + *

    + * Week one(1) is the week starting on the {@link WeekFields#getFirstDayOfWeek} + * where there are at least {@link WeekFields#getMinimalDaysInFirstWeek()} days in the year. + * If the first week starts after the start of the year then the period before + * is in the last week of the previous year. + *

    + * For example:
    + * - if the 1st day of the year is a Monday, week one starts on the 1st
    + * - if the 2nd day of the year is a Monday, week one starts on the 2nd and + * the 1st is in the last week of the previous year
    + * - if the 4th day of the year is a Monday, week one starts on the 4th and + * the 1st to 3rd is in the last week of the previous year
    + * - if the 5th day of the year is a Monday, week two starts on the 5th and + * the 1st to 4th is in week one
    + *

    + * This field can be used with any calendar system. + * @return a TemporalField to access the week of week-based-year, not null + */ + public TemporalField weekOfWeekBasedYear() { + return weekOfWeekBasedYear; + } + + /** + * Returns a field to access the year of a week-based-year, + * computed based on this WeekFields. + *

    + * This represents the concept of the year where weeks start on a fixed day-of-week, + * such as Monday and each week belongs to exactly one year. + * This field is typically used with {@link WeekFields#dayOfWeek()} and + * {@link WeekFields#weekOfWeekBasedYear()}. + *

    + * Week one(1) is the week starting on the {@link WeekFields#getFirstDayOfWeek} + * where there are at least {@link WeekFields#getMinimalDaysInFirstWeek()} days in the year. + * Thus, week one may start before the start of the year. + * If the first week starts after the start of the year then the period before + * is in the last week of the previous year. + *

    + * This field can be used with any calendar system. + * @return a TemporalField to access the year of week-based-year, not null + */ + public TemporalField weekBasedYear() { + return weekBasedYear; + } + + /** + * Checks if this WeekFields is equal to the specified object. *

    * The comparison is based on the entire state of the rules, which is * the first day-of-week and minimal days. @@ -469,6 +590,49 @@ static ComputedDayOfField ofWeekOfYearField(WeekFields weekDef) { return new ComputedDayOfField("WeekOfYear", weekDef, WEEKS, YEARS, WEEK_OF_YEAR_RANGE); } + + /** + * Returns a field to access the week of week-based-year, + * computed based on a WeekFields. + * @see WeekFields#weekOfWeekBasedYear() + */ + static ComputedDayOfField ofWeekOfWeekBasedYearField(WeekFields weekDef) { + return new ComputedDayOfField("WeekOfWeekBasedYear", weekDef, WEEKS, IsoFields.WEEK_BASED_YEARS, WEEK_OF_YEAR_RANGE); + } + + /** + * Returns a field to access the week of week-based-year, + * computed based on a WeekFields. + * @see WeekFields#weekBasedYear() + */ + static ComputedDayOfField ofWeekBasedYearField(WeekFields weekDef) { + return new ComputedDayOfField("WeekBasedYear", weekDef, IsoFields.WEEK_BASED_YEARS, FOREVER, ChronoField.YEAR.range()); + } + + /** + * Return a new week-based-year date of the Chronology, year, week-of-year, + * and dow of week. + * @param chrono The chronology of the new date + * @param yowby the year of the week-based-year + * @param wowby the week of the week-based-year + * @param dow the day of the week + * @return a ChronoLocalDate for the requested year, week of year, and day of week + */ + private ChronoLocalDate ofWeekBasedYear(Chronology chrono, + int yowby, int wowby, int dow) { + ChronoLocalDate date = chrono.date(yowby, 1, 1); + int ldow = localizedDayOfWeek(date); + int offset = startOfWeekOffset(1, ldow); + + // Clamp the week of year to keep it in the same year + int yearLen = date.lengthOfYear(); + int newYearWeek = computeWeek(offset, yearLen + weekDef.getMinimalDaysInFirstWeek()); + wowby = Math.min(wowby, newYearWeek - 1); + + int days = -offset + (dow - 1) + (wowby - 1) * 7; + return date.plus(days, DAYS); + } + private final String name; private final WeekFields weekDef; private final TemporalUnit baseUnit; @@ -489,44 +653,109 @@ @Override public long getFrom(TemporalAccessor temporal) { - // Offset the ISO DOW by the start of this week - int sow = weekDef.getFirstDayOfWeek().getValue(); - int dow = localizedDayOfWeek(temporal, sow); - if (rangeUnit == WEEKS) { // day-of-week - return dow; + return localizedDayOfWeek(temporal); } else if (rangeUnit == MONTHS) { // week-of-month - return localizedWeekOfMonth(temporal, dow); + return localizedWeekOfMonth(temporal); } else if (rangeUnit == YEARS) { // week-of-year - return localizedWeekOfYear(temporal, dow); + return localizedWeekOfYear(temporal); + } else if (rangeUnit == WEEK_BASED_YEARS) { + return localizedWeekOfWeekBasedYear(temporal); + } else if (rangeUnit == FOREVER) { + return localizedWeekBasedYear(temporal); } else { - throw new IllegalStateException("unreachable"); + throw new IllegalStateException("unreachable, rangeUnit: " + rangeUnit + ", this: " + this); } } - private int localizedDayOfWeek(TemporalAccessor temporal, int sow) { + private int localizedDayOfWeek(TemporalAccessor temporal) { + int sow = weekDef.getFirstDayOfWeek().getValue(); int isoDow = temporal.get(DAY_OF_WEEK); return Math.floorMod(isoDow - sow, 7) + 1; } - private long localizedWeekOfMonth(TemporalAccessor temporal, int dow) { + private long localizedWeekOfMonth(TemporalAccessor temporal) { + int dow = localizedDayOfWeek(temporal); int dom = temporal.get(DAY_OF_MONTH); int offset = startOfWeekOffset(dom, dow); return computeWeek(offset, dom); } - private long localizedWeekOfYear(TemporalAccessor temporal, int dow) { + private long localizedWeekOfYear(TemporalAccessor temporal) { + int dow = localizedDayOfWeek(temporal); int doy = temporal.get(DAY_OF_YEAR); int offset = startOfWeekOffset(doy, dow); return computeWeek(offset, doy); } /** + * Returns the year of week-based-year for the temporal. + * The year can be the previous year, the current year, or the next year. + * @param temporal a date of any chronology, not null + * @return the year of week-based-year for the date + */ + private int localizedWeekBasedYear(TemporalAccessor temporal) { + int dow = localizedDayOfWeek(temporal); + int year = temporal.get(YEAR); + int doy = temporal.get(DAY_OF_YEAR); + int offset = startOfWeekOffset(doy, dow); + int week = computeWeek(offset, doy); + if (week == 0) { + // Day is in end of week of previous year; return the previous year + return year - 1; + } else { + // If getting close to end of year, use higher precision logic + // Check if date of year is in partial week associated with next year + ValueRange dayRange = temporal.range(DAY_OF_YEAR); + int yearLen = (int)dayRange.getMaximum(); + int newYearWeek = computeWeek(offset, yearLen + weekDef.getMinimalDaysInFirstWeek()); + if (week >= newYearWeek) { + return year + 1; + } + } + return year; + } + + /** + * Returns the week of week-based-year for the temporal. + * The week can be part of the previous year, the current year, + * or the next year depending on the week start and minimum number + * of days. + * @param temporal a date of any chronology + * @return the week of the year + * @see #localizedWeekBasedYear(java.time.temporal.TemporalAccessor) + */ + private int localizedWeekOfWeekBasedYear(TemporalAccessor temporal) { + int dow = localizedDayOfWeek(temporal); + int doy = temporal.get(DAY_OF_YEAR); + int offset = startOfWeekOffset(doy, dow); + int week = computeWeek(offset, doy); + if (week == 0) { + // Day is in end of week of previous year + // Recompute from the last day of the previous year + ChronoLocalDate date = Chronology.from(temporal).date(temporal); + date = date.minus(doy, DAYS); // Back down into previous year + return localizedWeekOfWeekBasedYear(date); + } else if (week > 50) { + // If getting close to end of year, use higher precision logic + // Check if date of year is in partial week associated with next year + ValueRange dayRange = temporal.range(DAY_OF_YEAR); + int yearLen = (int)dayRange.getMaximum(); + int newYearWeek = computeWeek(offset, yearLen + weekDef.getMinimalDaysInFirstWeek()); + if (week >= newYearWeek) { + // Overlaps with week of following year; reduce to week in following year + week = week - newYearWeek + 1; + } + } + return week; + } + + /** * Returns an offset to align week start with a day of month or day of year. * - * @param day the day; 1 through infinity - * @param dow the day of the week of that day; 1 through 7 - * @return an offset in days to align a day with the start of the first 'full' week + * @param day the day; 1 through infinity + * @param dow the day of the week of that day; 1 through 7 + * @return an offset in days to align a day with the start of the first 'full' week */ private int startOfWeekOffset(int day, int dow) { // offset of first day corresponding to the day of week in first 7 days (zero origin) @@ -560,54 +789,81 @@ if (newVal == currentVal) { return temporal; } - // Compute the difference and add that using the base using of the field - return (R) temporal.plus(newVal - currentVal, baseUnit); + + if (rangeUnit == FOREVER) { // replace year of WeekBasedYear + // Create a new date object with the same chronology, + // the desired year and the same week and dow. + int idow = temporal.get(weekDef.dayOfWeek); + int wowby = temporal.get(weekDef.weekOfWeekBasedYear); + return (R) ofWeekBasedYear(Chronology.from(temporal), (int)newValue, wowby, idow); + } else { + // Compute the difference and add that using the base unit of the field + return (R) temporal.plus(newVal - currentVal, baseUnit); + } } @Override - public Map resolve(TemporalAccessor temporal, long value) { + public Map resolve(TemporalAccessor temporal, long value, ResolverStyle resolverStyle) { int newValue = range.checkValidIntValue(value, this); int sow = weekDef.getFirstDayOfWeek().getValue(); if (rangeUnit == WEEKS) { // day-of-week int isoDow = Math.floorMod((sow - 1) + (newValue - 1), 7) + 1; return Collections.singletonMap(DAY_OF_WEEK, (long) isoDow); } - if ((temporal.isSupported(YEAR) && temporal.isSupported(DAY_OF_WEEK)) == false) { + if (temporal.isSupported(DAY_OF_WEEK) == false) { return null; } - int dow = localizedDayOfWeek(temporal, sow); - int year = temporal.get(YEAR); Chronology chrono = Chronology.from(temporal); // defaults to ISO - if (rangeUnit == MONTHS) { // week-of-month - if (temporal.isSupported(MONTH_OF_YEAR) == false) { - return null; + int dow = localizedDayOfWeek(temporal); + if (temporal.isSupported(YEAR)) { + int year = temporal.get(YEAR); + if (rangeUnit == MONTHS) { // week-of-month + if (temporal.isSupported(MONTH_OF_YEAR) == false) { + return null; + } + int month = temporal.get(ChronoField.MONTH_OF_YEAR); + @SuppressWarnings("rawtypes") + ChronoLocalDate date = chrono.date(year, month, 1); + int dateDow = localizedDayOfWeek(date); + long weeks = newValue - localizedWeekOfMonth(date); + int days = dow - dateDow; + date = date.plus(weeks * 7 + days, DAYS); + Map result = new HashMap<>(4, 1.0f); + result.put(EPOCH_DAY, date.toEpochDay()); + result.put(YEAR, null); + result.put(MONTH_OF_YEAR, null); + result.put(DAY_OF_WEEK, null); + return result; + } else if (rangeUnit == YEARS) { // week-of-year + @SuppressWarnings("rawtypes") + ChronoLocalDate date = chrono.date(year, 1, 1); + int dateDow = localizedDayOfWeek(date); + long weeks = newValue - localizedWeekOfYear(date); + int days = dow - dateDow; + date = date.plus(weeks * 7 + days, DAYS); + Map result = new HashMap<>(4, 1.0f); + result.put(EPOCH_DAY, date.toEpochDay()); + result.put(YEAR, null); + result.put(DAY_OF_WEEK, null); + return result; + } + } else if (rangeUnit == WEEK_BASED_YEARS || rangeUnit == FOREVER) { + if (temporal.isSupported(weekDef.weekBasedYear) && + temporal.isSupported(weekDef.weekOfWeekBasedYear)) { + // week-of-week-based-year and year-of-week-based-year + int yowby = temporal.get(weekDef.weekBasedYear); + int wowby = temporal.get(weekDef.weekOfWeekBasedYear); + ChronoLocalDate date = ofWeekBasedYear(Chronology.from(temporal), yowby, wowby, dow); + + Map result = new HashMap<>(4, 1.0f); + result.put(EPOCH_DAY, date.toEpochDay()); + result.put(DAY_OF_WEEK, null); + result.put(weekDef.weekOfWeekBasedYear, null); + result.put(weekDef.weekBasedYear, null); + return result; } - int month = temporal.get(ChronoField.MONTH_OF_YEAR); - ChronoLocalDate date = chrono.date(year, month, 1); - int dateDow = localizedDayOfWeek(date, sow); - long weeks = newValue - localizedWeekOfMonth(date, dateDow); - int days = dow - dateDow; - date = date.plus(weeks * 7 + days, DAYS); - Map result = new HashMap<>(4, 1.0f); - result.put(EPOCH_DAY, date.toEpochDay()); - result.put(YEAR, null); - result.put(MONTH_OF_YEAR, null); - result.put(DAY_OF_WEEK, null); - return result; - } else if (rangeUnit == YEARS) { // week-of-year - ChronoLocalDate date = chrono.date(year, 1, 1); - int dateDow = localizedDayOfWeek(date, sow); - long weeks = newValue - localizedWeekOfYear(date, dateDow); - int days = dow - dateDow; - date = date.plus(weeks * 7 + days, DAYS); - Map result = new HashMap<>(4, 1.0f); - result.put(EPOCH_DAY, date.toEpochDay()); - result.put(YEAR, null); - result.put(DAY_OF_WEEK, null); - return result; - } else { - throw new IllegalStateException("unreachable"); } + return null; } //----------------------------------------------------------------------- @@ -617,6 +873,18 @@ } @Override + public String getDisplayName(Locale locale) { + Objects.requireNonNull(locale, "locale"); + if (rangeUnit == YEARS) { // only have values for week-of-year + LocaleResources lr = LocaleProviderAdapter.getResourceBundleBased() + .getLocaleResources(locale); + ResourceBundle rb = lr.getJavaTimeFormatData(); + return rb.containsKey("field.week") ? rb.getString("field.week") : getName(); + } + return getName(); + } + + @Override public TemporalUnit getBaseUnit() { return baseUnit; } @@ -627,6 +895,11 @@ } @Override + public boolean isDateBased() { + return true; + } + + @Override public ValueRange range() { return range; } @@ -641,6 +914,10 @@ return temporal.isSupported(DAY_OF_MONTH); } else if (rangeUnit == YEARS) { // week-of-year return temporal.isSupported(DAY_OF_YEAR); + } else if (rangeUnit == WEEK_BASED_YEARS) { + return temporal.isSupported(DAY_OF_YEAR); + } else if (rangeUnit == FOREVER) { + return temporal.isSupported(YEAR); } } return false; @@ -650,27 +927,68 @@ public ValueRange rangeRefinedBy(TemporalAccessor temporal) { if (rangeUnit == ChronoUnit.WEEKS) { // day-of-week return range; - } - - TemporalField field = null; - if (rangeUnit == MONTHS) { // week-of-month - field = DAY_OF_MONTH; + } else if (rangeUnit == MONTHS) { // week-of-month + return rangeByWeek(temporal, DAY_OF_MONTH); } else if (rangeUnit == YEARS) { // week-of-year - field = DAY_OF_YEAR; + return rangeByWeek(temporal, DAY_OF_YEAR); + } else if (rangeUnit == WEEK_BASED_YEARS) { + return rangeWeekOfWeekBasedYear(temporal); + } else if (rangeUnit == FOREVER) { + return YEAR.range(); } else { - throw new IllegalStateException("unreachable"); + throw new IllegalStateException("unreachable, rangeUnit: " + rangeUnit + ", this: " + this); } + } - // Offset the ISO DOW by the start of this week - int sow = weekDef.getFirstDayOfWeek().getValue(); - int dow = localizedDayOfWeek(temporal, sow); - + /** + * Map the field range to a week range + * @param temporal the temporal + * @param field the field to get the range of + * @return the ValueRange with the range adjusted to weeks. + */ + private ValueRange rangeByWeek(TemporalAccessor temporal, TemporalField field) { + int dow = localizedDayOfWeek(temporal); int offset = startOfWeekOffset(temporal.get(field), dow); ValueRange fieldRange = temporal.range(field); return ValueRange.of(computeWeek(offset, (int) fieldRange.getMinimum()), computeWeek(offset, (int) fieldRange.getMaximum())); } + /** + * Map the field range to a week range of a week year. + * @param temporal the temporal + * @param field the field to get the range of + * @return the ValueRange with the range adjusted to weeks. + */ + private ValueRange rangeWeekOfWeekBasedYear(TemporalAccessor temporal) { + if (!temporal.isSupported(DAY_OF_YEAR)) { + return WEEK_OF_YEAR_RANGE; + } + int dow = localizedDayOfWeek(temporal); + int doy = temporal.get(DAY_OF_YEAR); + int offset = startOfWeekOffset(doy, dow); + int week = computeWeek(offset, doy); + if (week == 0) { + // Day is in end of week of previous year + // Recompute from the last day of the previous year + ChronoLocalDate date = Chronology.from(temporal).date(temporal); + date = date.minus(doy + 7, DAYS); // Back down into previous year + return rangeWeekOfWeekBasedYear(date); + } + // Check if day of year is in partial week associated with next year + ValueRange dayRange = temporal.range(DAY_OF_YEAR); + int yearLen = (int)dayRange.getMaximum(); + int newYearWeek = computeWeek(offset, yearLen + weekDef.getMinimalDaysInFirstWeek()); + + if (week >= newYearWeek) { + // Overlaps with weeks of following year; recompute from a week in following year + ChronoLocalDate date = Chronology.from(temporal).date(temporal); + date = date.plus(yearLen - doy + 1 + 7, ChronoUnit.DAYS); + return rangeWeekOfWeekBasedYear(date); + } + return ValueRange.of(1, newYearWeek-1); + } + //----------------------------------------------------------------------- @Override public String toString() {