src/share/classes/java/time/temporal/WeekFields.java
Print this page
@@ -66,37 +66,43 @@
import static java.time.temporal.ChronoField.DAY_OF_YEAR;
import static java.time.temporal.ChronoField.EPOCH_DAY;
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;
import java.io.InvalidObjectException;
import java.io.Serializable;
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.
* <p>
* A standard week is seven days long, but cultures have different definitions for some
* other aspects of a week. This class represents the definition of the week, for the
* purpose of providing {@link TemporalField} instances.
* <p>
- * 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}.
* <p>
* The computations for day-of-week, week-of-month, and week-of-year are based
* on the {@linkplain ChronoField#YEAR proleptic-year},
* {@linkplain ChronoField#MONTH_OF_YEAR month-of-year},
@@ -108,11 +114,11 @@
* <p>A week is defined by:
* <ul>
* <li>The first day-of-week.
* For example, the ISO-8601 standard considers Monday to be the first day-of-week.
* <li>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.
* </ul><p>
* Together these two values allow a year or month to be divided into weeks.
* <p>
* <h3>Week of Month</h3>
* One field is used: week-of-month.
@@ -132,18 +138,41 @@
* <tr><th>2009-01-04</th><td>Sunday</td>
* <td>Week 1 of January 2009</td><td>Week 0 of January 2009</td></tr>
* <tr><th>2009-01-05</th><td>Monday</td>
* <td>Week 2 of January 2009</td><td>Week 1 of January 2009</td></tr>
* </table>
- * <p>
+ *
* <h3>Week of Year</h3>
* 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.
- * <p>
+ *
+ * <h3>Week Based Year</h3>
+ * 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.
+ *
+ * <table cellpadding="0" cellspacing="3" border="0" style="text-align: left; width: 50%;">
+ * <caption>Examples of WeekFields for week-based-year</caption>
+ * <tr><th>Date</th><td>Day-of-week</td>
+ * <td>First day: Monday<br>Minimal days: 4</td><td>First day: Monday<br>Minimal days: 5</td></tr>
+ * <tr><th>2008-12-31</th><td>Wednesday</td>
+ * <td>Week 1 of 2009</td><td>Week 53 of 2008</td></tr>
+ * <tr><th>2009-01-01</th><td>Thursday</td>
+ * <td>Week 1 of 2009</td><td>Week 53 of 2008</td></tr>
+ * <tr><th>2009-01-04</th><td>Sunday</td>
+ * <td>Week 1 of 2009</td><td>Week 53 of 2008</td></tr>
+ * <tr><th>2009-01-05</th><td>Monday</td>
+ * <td>Week 2 of 2009</td><td>Week 1 of 2009</td></tr>
+ * </table>
+ * <h3>Specification for implementors</h3>
* This class is immutable and thread-safe.
*
* @since 1.8
*/
public final class WeekFields implements Serializable {
@@ -169,23 +198,45 @@
* up the passage of days instead of the standard year/month/day.
* <p>
* 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.
+ * <p>
+ * 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.
* <p>
* 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.
- *
+ * <p>
+ * 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.
+ * <p>
+ * 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}.
+ * <p>
+ * 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.
+ * <p>
+ * 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;
/**
@@ -211,10 +262,28 @@
* The field used to access the computed WeekOfYear.
*/
private transient final TemporalField weekOfYear = ComputedDayOfField.ofWeekOfYearField(this);
/**
+ * The field that represents the week-of-week-based-year.
+ * <p>
+ * This field allows the week of the week-based-year value to be queried and set.
+ * <p>
+ * 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.
+ * <p>
+ * This field allows the week-based-year value to be queried and set.
+ * <p>
+ * 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.
* <p>
* This will look up appropriate values from the provider of localization data.
*
* @param locale the locale to use, not null
@@ -339,11 +408,11 @@
/**
* Returns a field to access the week of month,
* computed based on this WeekFields.
* <p>
- * 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()}.
* <p>
* Week one (1) is the week starting on the {@link WeekFields#getFirstDayOfWeek}
* where there are at least {@link WeekFields#getMinimalDaysInFirstWeek()} days in the month.
@@ -365,16 +434,16 @@
/**
* Returns a field to access the week of year,
* computed based on this WeekFields.
* <p>
- * 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()}.
* <p>
* 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).
* <p>
* For example:<br>
* - if the 1st day of the year is a Monday, week one starts on the 1st and there is no week zero<br>
@@ -388,11 +457,63 @@
public TemporalField weekOfYear() {
return weekOfYear;
}
/**
- * 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.
+ * <p>
+ * 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()}.
+ * <p>
+ * 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.
+ * <p>
+ * For example:<br>
+ * - if the 1st day of the year is a Monday, week one starts on the 1st<br>
+ * - 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<br>
+ * - 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<br>
+ * - 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<br>
+ * <p>
+ * 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.
+ * <p>
+ * 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()}.
+ * <p>
+ * 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.
+ * <p>
+ * 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.
* <p>
* The comparison is based on the entire state of the rules, which is
* the first day-of-week and minimal days.
*
* @param object the other rules to compare to, null returns false
@@ -467,10 +588,53 @@
* @see WeekFields#weekOfYear()
*/
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;
private final TemporalUnit rangeUnit;
private final ValueRange range;
@@ -487,43 +651,108 @@
private static final ValueRange WEEK_OF_MONTH_RANGE = ValueRange.of(0, 1, 4, 6);
private static final ValueRange WEEK_OF_YEAR_RANGE = ValueRange.of(0, 1, 52, 54);
@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
@@ -558,77 +787,121 @@
int newVal = range.checkValidIntValue(newValue, this); // lenient check range
int currentVal = temporal.get(this);
if (newVal == currentVal) {
return temporal;
}
- // Compute the difference and add that using the base using of the field
+
+ 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<TemporalField, Long> resolve(TemporalAccessor temporal, long value) {
+ public Map<TemporalField, Long> 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.<TemporalField, Long>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
+ 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, sow);
- long weeks = newValue - localizedWeekOfMonth(date, dateDow);
+ int dateDow = localizedDayOfWeek(date);
+ long weeks = newValue - localizedWeekOfMonth(date);
int days = dow - dateDow;
date = date.plus(weeks * 7 + days, DAYS);
Map<TemporalField, Long> 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, sow);
- long weeks = newValue - localizedWeekOfYear(date, dateDow);
+ int dateDow = localizedDayOfWeek(date);
+ long weeks = newValue - localizedWeekOfYear(date);
int days = dow - dateDow;
date = date.plus(weeks * 7 + days, DAYS);
Map<TemporalField, Long> 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");
+ }
+ } 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<TemporalField, Long> 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;
}
}
+ return null;
+ }
//-----------------------------------------------------------------------
@Override
public String getName() {
return name;
}
@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;
}
@Override
public TemporalUnit getRangeUnit() {
return rangeUnit;
}
@Override
+ public boolean isDateBased() {
+ return true;
+ }
+
+ @Override
public ValueRange range() {
return range;
}
//-----------------------------------------------------------------------
@@ -639,40 +912,85 @@
return true;
} else if (rangeUnit == MONTHS) { // week-of-month
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;
}
@Override
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() {
return getName() + "[" + weekDef.toString() + "]";
}