--- 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 @@ *
* Together these two values allow a year or month to be divided into weeks. *
@@ -134,14 +140,37 @@ *
+ * *
+ * + *
Date | Day-of-week | + *First day: Monday Minimal days: 4 | First day: Monday Minimal days: 5 |
---|---|---|---|
2008-12-31 | Wednesday | + *Week 1 of 2009 | Week 53 of 2008 |
2009-01-01 | Thursday | + *Week 1 of 2009 | Week 53 of 2008 |
2009-01-04 | Sunday | + *Week 1 of 2009 | Week 53 of 2008 |
2009-01-05 | Monday | + *Week 2 of 2009 | Week 1 of 2009 |
+ * 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