15289
|
1 |
/*
|
|
2 |
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
/*
|
|
27 |
* This file is available under and governed by the GNU General Public
|
|
28 |
* License version 2 only, as published by the Free Software Foundation.
|
|
29 |
* However, the following notice accompanied the original version of this
|
|
30 |
* file:
|
|
31 |
*
|
|
32 |
* Copyright (c) 2011-2012, Stephen Colebourne & Michael Nascimento Santos
|
|
33 |
*
|
|
34 |
* All rights reserved.
|
|
35 |
*
|
|
36 |
* Redistribution and use in source and binary forms, with or without
|
|
37 |
* modification, are permitted provided that the following conditions are met:
|
|
38 |
*
|
|
39 |
* * Redistributions of source code must retain the above copyright notice,
|
|
40 |
* this list of conditions and the following disclaimer.
|
|
41 |
*
|
|
42 |
* * Redistributions in binary form must reproduce the above copyright notice,
|
|
43 |
* this list of conditions and the following disclaimer in the documentation
|
|
44 |
* and/or other materials provided with the distribution.
|
|
45 |
*
|
|
46 |
* * Neither the name of JSR-310 nor the names of its contributors
|
|
47 |
* may be used to endorse or promote products derived from this software
|
|
48 |
* without specific prior written permission.
|
|
49 |
*
|
|
50 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
51 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
52 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
53 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
54 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
55 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
56 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
57 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
58 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
59 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
60 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
61 |
*/
|
|
62 |
package java.time.temporal;
|
|
63 |
|
15658
|
64 |
import static java.time.temporal.ChronoField.DAY_OF_MONTH;
|
|
65 |
import static java.time.temporal.ChronoField.DAY_OF_WEEK;
|
|
66 |
import static java.time.temporal.ChronoField.DAY_OF_YEAR;
|
|
67 |
import static java.time.temporal.ChronoField.EPOCH_DAY;
|
|
68 |
import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
|
|
69 |
import static java.time.temporal.ChronoField.YEAR;
|
|
70 |
import static java.time.temporal.ChronoUnit.DAYS;
|
16852
|
71 |
import static java.time.temporal.ChronoUnit.FOREVER;
|
15658
|
72 |
import static java.time.temporal.ChronoUnit.MONTHS;
|
|
73 |
import static java.time.temporal.ChronoUnit.WEEKS;
|
|
74 |
import static java.time.temporal.ChronoUnit.YEARS;
|
|
75 |
|
15289
|
76 |
import java.io.InvalidObjectException;
|
|
77 |
import java.io.Serializable;
|
19030
|
78 |
import java.time.DateTimeException;
|
15289
|
79 |
import java.time.DayOfWeek;
|
19030
|
80 |
import java.time.ZoneId;
|
15658
|
81 |
import java.time.chrono.ChronoLocalDate;
|
|
82 |
import java.time.chrono.Chronology;
|
16852
|
83 |
import java.time.format.ResolverStyle;
|
15658
|
84 |
import java.util.Collections;
|
|
85 |
import java.util.HashMap;
|
15289
|
86 |
import java.util.Locale;
|
15658
|
87 |
import java.util.Map;
|
15289
|
88 |
import java.util.Objects;
|
16852
|
89 |
import java.util.ResourceBundle;
|
15289
|
90 |
import java.util.concurrent.ConcurrentHashMap;
|
|
91 |
import java.util.concurrent.ConcurrentMap;
|
15658
|
92 |
import sun.util.locale.provider.CalendarDataUtility;
|
16852
|
93 |
import sun.util.locale.provider.LocaleProviderAdapter;
|
|
94 |
import sun.util.locale.provider.LocaleResources;
|
15289
|
95 |
|
|
96 |
/**
|
|
97 |
* Localized definitions of the day-of-week, week-of-month and week-of-year fields.
|
|
98 |
* <p>
|
|
99 |
* A standard week is seven days long, but cultures have different definitions for some
|
|
100 |
* other aspects of a week. This class represents the definition of the week, for the
|
|
101 |
* purpose of providing {@link TemporalField} instances.
|
|
102 |
* <p>
|
16852
|
103 |
* WeekFields provides five fields,
|
|
104 |
* {@link #dayOfWeek()}, {@link #weekOfMonth()}, {@link #weekOfYear()},
|
|
105 |
* {@link #weekOfWeekBasedYear()}, and {@link #weekBasedYear()}
|
15289
|
106 |
* that provide access to the values from any {@linkplain Temporal temporal object}.
|
|
107 |
* <p>
|
|
108 |
* The computations for day-of-week, week-of-month, and week-of-year are based
|
|
109 |
* on the {@linkplain ChronoField#YEAR proleptic-year},
|
|
110 |
* {@linkplain ChronoField#MONTH_OF_YEAR month-of-year},
|
|
111 |
* {@linkplain ChronoField#DAY_OF_MONTH day-of-month}, and
|
|
112 |
* {@linkplain ChronoField#DAY_OF_WEEK ISO day-of-week} which are based on the
|
|
113 |
* {@linkplain ChronoField#EPOCH_DAY epoch-day} and the chronology.
|
|
114 |
* The values may not be aligned with the {@linkplain ChronoField#YEAR_OF_ERA year-of-Era}
|
|
115 |
* depending on the Chronology.
|
|
116 |
* <p>A week is defined by:
|
|
117 |
* <ul>
|
|
118 |
* <li>The first day-of-week.
|
|
119 |
* For example, the ISO-8601 standard considers Monday to be the first day-of-week.
|
|
120 |
* <li>The minimal number of days in the first week.
|
16852
|
121 |
* For example, the ISO-8601 standard counts the first week as needing at least 4 days.
|
15289
|
122 |
* </ul><p>
|
|
123 |
* Together these two values allow a year or month to be divided into weeks.
|
|
124 |
* <p>
|
|
125 |
* <h3>Week of Month</h3>
|
|
126 |
* One field is used: week-of-month.
|
|
127 |
* The calculation ensures that weeks never overlap a month boundary.
|
|
128 |
* The month is divided into periods where each period starts on the defined first day-of-week.
|
|
129 |
* The earliest period is referred to as week 0 if it has less than the minimal number of days
|
|
130 |
* and week 1 if it has at least the minimal number of days.
|
|
131 |
* <p>
|
|
132 |
* <table cellpadding="0" cellspacing="3" border="0" style="text-align: left; width: 50%;">
|
|
133 |
* <caption>Examples of WeekFields</caption>
|
|
134 |
* <tr><th>Date</th><td>Day-of-week</td>
|
|
135 |
* <td>First day: Monday<br>Minimal days: 4</td><td>First day: Monday<br>Minimal days: 5</td></tr>
|
|
136 |
* <tr><th>2008-12-31</th><td>Wednesday</td>
|
|
137 |
* <td>Week 5 of December 2008</td><td>Week 5 of December 2008</td></tr>
|
|
138 |
* <tr><th>2009-01-01</th><td>Thursday</td>
|
|
139 |
* <td>Week 1 of January 2009</td><td>Week 0 of January 2009</td></tr>
|
|
140 |
* <tr><th>2009-01-04</th><td>Sunday</td>
|
|
141 |
* <td>Week 1 of January 2009</td><td>Week 0 of January 2009</td></tr>
|
|
142 |
* <tr><th>2009-01-05</th><td>Monday</td>
|
|
143 |
* <td>Week 2 of January 2009</td><td>Week 1 of January 2009</td></tr>
|
|
144 |
* </table>
|
16852
|
145 |
*
|
15289
|
146 |
* <h3>Week of Year</h3>
|
|
147 |
* One field is used: week-of-year.
|
|
148 |
* The calculation ensures that weeks never overlap a year boundary.
|
|
149 |
* The year is divided into periods where each period starts on the defined first day-of-week.
|
|
150 |
* The earliest period is referred to as week 0 if it has less than the minimal number of days
|
|
151 |
* and week 1 if it has at least the minimal number of days.
|
16852
|
152 |
*
|
|
153 |
* <h3>Week Based Year</h3>
|
|
154 |
* Two fields are used for week-based-year, one for the
|
|
155 |
* {@link #weekOfWeekBasedYear() week-of-week-based-year} and one for
|
|
156 |
* {@link #weekBasedYear() week-based-year}. In a week-based-year, each week
|
|
157 |
* belongs to only a single year. Week 1 of a year is the first week that
|
|
158 |
* starts on the first day-of-week and has at least the minimum number of days.
|
|
159 |
* The first and last weeks of a year may contain days from the
|
|
160 |
* previous calendar year or next calendar year respectively.
|
|
161 |
*
|
|
162 |
* <table cellpadding="0" cellspacing="3" border="0" style="text-align: left; width: 50%;">
|
|
163 |
* <caption>Examples of WeekFields for week-based-year</caption>
|
|
164 |
* <tr><th>Date</th><td>Day-of-week</td>
|
|
165 |
* <td>First day: Monday<br>Minimal days: 4</td><td>First day: Monday<br>Minimal days: 5</td></tr>
|
|
166 |
* <tr><th>2008-12-31</th><td>Wednesday</td>
|
|
167 |
* <td>Week 1 of 2009</td><td>Week 53 of 2008</td></tr>
|
|
168 |
* <tr><th>2009-01-01</th><td>Thursday</td>
|
|
169 |
* <td>Week 1 of 2009</td><td>Week 53 of 2008</td></tr>
|
|
170 |
* <tr><th>2009-01-04</th><td>Sunday</td>
|
|
171 |
* <td>Week 1 of 2009</td><td>Week 53 of 2008</td></tr>
|
|
172 |
* <tr><th>2009-01-05</th><td>Monday</td>
|
|
173 |
* <td>Week 2 of 2009</td><td>Week 1 of 2009</td></tr>
|
|
174 |
* </table>
|
17474
|
175 |
*
|
|
176 |
* @implSpec
|
15289
|
177 |
* This class is immutable and thread-safe.
|
|
178 |
*
|
|
179 |
* @since 1.8
|
|
180 |
*/
|
|
181 |
public final class WeekFields implements Serializable {
|
|
182 |
// implementation notes
|
|
183 |
// querying week-of-month or week-of-year should return the week value bound within the month/year
|
|
184 |
// however, setting the week value should be lenient (use plus/minus weeks)
|
15658
|
185 |
// allow week-of-month outer range [0 to 6]
|
|
186 |
// allow week-of-year outer range [0 to 54]
|
15289
|
187 |
// this is because callers shouldn't be expected to know the details of validity
|
|
188 |
|
|
189 |
/**
|
|
190 |
* The cache of rules by firstDayOfWeek plus minimalDays.
|
|
191 |
* Initialized first to be available for definition of ISO, etc.
|
|
192 |
*/
|
|
193 |
private static final ConcurrentMap<String, WeekFields> CACHE = new ConcurrentHashMap<>(4, 0.75f, 2);
|
|
194 |
|
|
195 |
/**
|
|
196 |
* The ISO-8601 definition, where a week starts on Monday and the first week
|
|
197 |
* has a minimum of 4 days.
|
|
198 |
* <p>
|
|
199 |
* The ISO-8601 standard defines a calendar system based on weeks.
|
|
200 |
* It uses the week-based-year and week-of-week-based-year concepts to split
|
|
201 |
* up the passage of days instead of the standard year/month/day.
|
|
202 |
* <p>
|
|
203 |
* Note that the first week may start in the previous calendar year.
|
|
204 |
* Note also that the first few days of a calendar year may be in the
|
|
205 |
* week-based-year corresponding to the previous calendar year.
|
|
206 |
*/
|
|
207 |
public static final WeekFields ISO = new WeekFields(DayOfWeek.MONDAY, 4);
|
|
208 |
|
|
209 |
/**
|
16852
|
210 |
* The common definition of a week that starts on Sunday and the first week
|
|
211 |
* has a minimum of 1 day.
|
15289
|
212 |
* <p>
|
|
213 |
* Defined as starting on Sunday and with a minimum of 1 day in the month.
|
|
214 |
* This week definition is in use in the US and other European countries.
|
|
215 |
*/
|
|
216 |
public static final WeekFields SUNDAY_START = WeekFields.of(DayOfWeek.SUNDAY, 1);
|
|
217 |
|
|
218 |
/**
|
16852
|
219 |
* The unit that represents week-based-years for the purpose of addition and subtraction.
|
|
220 |
* <p>
|
|
221 |
* This allows a number of week-based-years to be added to, or subtracted from, a date.
|
|
222 |
* The unit is equal to either 52 or 53 weeks.
|
|
223 |
* The estimated duration of a week-based-year is the same as that of a standard ISO
|
|
224 |
* year at {@code 365.2425 Days}.
|
|
225 |
* <p>
|
|
226 |
* The rules for addition add the number of week-based-years to the existing value
|
|
227 |
* for the week-based-year field retaining the week-of-week-based-year
|
|
228 |
* and day-of-week, unless the week number it too large for the target year.
|
|
229 |
* In that case, the week is set to the last week of the year
|
|
230 |
* with the same day-of-week.
|
|
231 |
* <p>
|
17474
|
232 |
* This unit is an immutable and thread-safe singleton.
|
16852
|
233 |
*/
|
|
234 |
public static final TemporalUnit WEEK_BASED_YEARS = IsoFields.WEEK_BASED_YEARS;
|
|
235 |
|
|
236 |
/**
|
15289
|
237 |
* Serialization version.
|
|
238 |
*/
|
|
239 |
private static final long serialVersionUID = -1177360819670808121L;
|
|
240 |
|
|
241 |
/**
|
|
242 |
* The first day-of-week.
|
|
243 |
*/
|
|
244 |
private final DayOfWeek firstDayOfWeek;
|
|
245 |
/**
|
|
246 |
* The minimal number of days in the first week.
|
|
247 |
*/
|
|
248 |
private final int minimalDays;
|
|
249 |
/**
|
|
250 |
* The field used to access the computed DayOfWeek.
|
|
251 |
*/
|
|
252 |
private transient final TemporalField dayOfWeek = ComputedDayOfField.ofDayOfWeekField(this);
|
|
253 |
/**
|
|
254 |
* The field used to access the computed WeekOfMonth.
|
|
255 |
*/
|
|
256 |
private transient final TemporalField weekOfMonth = ComputedDayOfField.ofWeekOfMonthField(this);
|
|
257 |
/**
|
|
258 |
* The field used to access the computed WeekOfYear.
|
|
259 |
*/
|
|
260 |
private transient final TemporalField weekOfYear = ComputedDayOfField.ofWeekOfYearField(this);
|
|
261 |
/**
|
16852
|
262 |
* The field that represents the week-of-week-based-year.
|
|
263 |
* <p>
|
|
264 |
* This field allows the week of the week-based-year value to be queried and set.
|
|
265 |
* <p>
|
|
266 |
* This unit is an immutable and thread-safe singleton.
|
|
267 |
*/
|
|
268 |
private transient final TemporalField weekOfWeekBasedYear = ComputedDayOfField.ofWeekOfWeekBasedYearField(this);
|
|
269 |
/**
|
|
270 |
* The field that represents the week-based-year.
|
|
271 |
* <p>
|
|
272 |
* This field allows the week-based-year value to be queried and set.
|
|
273 |
* <p>
|
|
274 |
* This unit is an immutable and thread-safe singleton.
|
|
275 |
*/
|
|
276 |
private transient final TemporalField weekBasedYear = ComputedDayOfField.ofWeekBasedYearField(this);
|
|
277 |
|
17474
|
278 |
//-----------------------------------------------------------------------
|
16852
|
279 |
/**
|
15289
|
280 |
* Obtains an instance of {@code WeekFields} appropriate for a locale.
|
|
281 |
* <p>
|
|
282 |
* This will look up appropriate values from the provider of localization data.
|
|
283 |
*
|
|
284 |
* @param locale the locale to use, not null
|
|
285 |
* @return the week-definition, not null
|
|
286 |
*/
|
|
287 |
public static WeekFields of(Locale locale) {
|
|
288 |
Objects.requireNonNull(locale, "locale");
|
|
289 |
locale = new Locale(locale.getLanguage(), locale.getCountry()); // elminate variants
|
|
290 |
|
15658
|
291 |
int calDow = CalendarDataUtility.retrieveFirstDayOfWeek(locale);
|
15289
|
292 |
DayOfWeek dow = DayOfWeek.SUNDAY.plus(calDow - 1);
|
15658
|
293 |
int minDays = CalendarDataUtility.retrieveMinimalDaysInFirstWeek(locale);
|
15289
|
294 |
return WeekFields.of(dow, minDays);
|
|
295 |
}
|
|
296 |
|
|
297 |
/**
|
|
298 |
* Obtains an instance of {@code WeekFields} from the first day-of-week and minimal days.
|
|
299 |
* <p>
|
|
300 |
* The first day-of-week defines the ISO {@code DayOfWeek} that is day 1 of the week.
|
|
301 |
* The minimal number of days in the first week defines how many days must be present
|
|
302 |
* in a month or year, starting from the first day-of-week, before the week is counted
|
|
303 |
* as the first week. A value of 1 will count the first day of the month or year as part
|
|
304 |
* of the first week, whereas a value of 7 will require the whole seven days to be in
|
|
305 |
* the new month or year.
|
|
306 |
* <p>
|
|
307 |
* WeekFields instances are singletons; for each unique combination
|
|
308 |
* of {@code firstDayOfWeek} and {@code minimalDaysInFirstWeek} the
|
|
309 |
* the same instance will be returned.
|
|
310 |
*
|
|
311 |
* @param firstDayOfWeek the first day of the week, not null
|
|
312 |
* @param minimalDaysInFirstWeek the minimal number of days in the first week, from 1 to 7
|
|
313 |
* @return the week-definition, not null
|
|
314 |
* @throws IllegalArgumentException if the minimal days value is less than one
|
|
315 |
* or greater than 7
|
|
316 |
*/
|
|
317 |
public static WeekFields of(DayOfWeek firstDayOfWeek, int minimalDaysInFirstWeek) {
|
|
318 |
String key = firstDayOfWeek.toString() + minimalDaysInFirstWeek;
|
|
319 |
WeekFields rules = CACHE.get(key);
|
|
320 |
if (rules == null) {
|
|
321 |
rules = new WeekFields(firstDayOfWeek, minimalDaysInFirstWeek);
|
|
322 |
CACHE.putIfAbsent(key, rules);
|
|
323 |
rules = CACHE.get(key);
|
|
324 |
}
|
|
325 |
return rules;
|
|
326 |
}
|
|
327 |
|
|
328 |
//-----------------------------------------------------------------------
|
|
329 |
/**
|
|
330 |
* Creates an instance of the definition.
|
|
331 |
*
|
|
332 |
* @param firstDayOfWeek the first day of the week, not null
|
|
333 |
* @param minimalDaysInFirstWeek the minimal number of days in the first week, from 1 to 7
|
|
334 |
* @throws IllegalArgumentException if the minimal days value is invalid
|
|
335 |
*/
|
|
336 |
private WeekFields(DayOfWeek firstDayOfWeek, int minimalDaysInFirstWeek) {
|
|
337 |
Objects.requireNonNull(firstDayOfWeek, "firstDayOfWeek");
|
|
338 |
if (minimalDaysInFirstWeek < 1 || minimalDaysInFirstWeek > 7) {
|
|
339 |
throw new IllegalArgumentException("Minimal number of days is invalid");
|
|
340 |
}
|
|
341 |
this.firstDayOfWeek = firstDayOfWeek;
|
|
342 |
this.minimalDays = minimalDaysInFirstWeek;
|
|
343 |
}
|
|
344 |
|
|
345 |
//-----------------------------------------------------------------------
|
|
346 |
/**
|
|
347 |
* Return the singleton WeekFields associated with the
|
|
348 |
* {@code firstDayOfWeek} and {@code minimalDays}.
|
|
349 |
* @return the singleton WeekFields for the firstDayOfWeek and minimalDays.
|
|
350 |
* @throws InvalidObjectException if the serialized object has invalid
|
|
351 |
* values for firstDayOfWeek or minimalDays.
|
|
352 |
*/
|
|
353 |
private Object readResolve() throws InvalidObjectException {
|
|
354 |
try {
|
|
355 |
return WeekFields.of(firstDayOfWeek, minimalDays);
|
|
356 |
} catch (IllegalArgumentException iae) {
|
17474
|
357 |
throw new InvalidObjectException("Invalid serialized WeekFields: " + iae.getMessage());
|
15289
|
358 |
}
|
|
359 |
}
|
|
360 |
|
|
361 |
//-----------------------------------------------------------------------
|
|
362 |
/**
|
|
363 |
* Gets the first day-of-week.
|
|
364 |
* <p>
|
|
365 |
* The first day-of-week varies by culture.
|
|
366 |
* For example, the US uses Sunday, while France and the ISO-8601 standard use Monday.
|
|
367 |
* This method returns the first day using the standard {@code DayOfWeek} enum.
|
|
368 |
*
|
|
369 |
* @return the first day-of-week, not null
|
|
370 |
*/
|
|
371 |
public DayOfWeek getFirstDayOfWeek() {
|
|
372 |
return firstDayOfWeek;
|
|
373 |
}
|
|
374 |
|
|
375 |
/**
|
|
376 |
* Gets the minimal number of days in the first week.
|
|
377 |
* <p>
|
|
378 |
* The number of days considered to define the first week of a month or year
|
|
379 |
* varies by culture.
|
|
380 |
* For example, the ISO-8601 requires 4 days (more than half a week) to
|
|
381 |
* be present before counting the first week.
|
|
382 |
*
|
|
383 |
* @return the minimal number of days in the first week of a month or year, from 1 to 7
|
|
384 |
*/
|
|
385 |
public int getMinimalDaysInFirstWeek() {
|
|
386 |
return minimalDays;
|
|
387 |
}
|
|
388 |
|
|
389 |
//-----------------------------------------------------------------------
|
|
390 |
/**
|
17474
|
391 |
* Returns a field to access the day of week based on this {@code WeekFields}.
|
15289
|
392 |
* <p>
|
17474
|
393 |
* This is similar to {@link ChronoField#DAY_OF_WEEK} but uses values for
|
|
394 |
* the day-of-week based on this {@code WeekFields}.
|
|
395 |
* The days are numbered from 1 to 7 where the
|
|
396 |
* {@link #getFirstDayOfWeek() first day-of-week} is assigned the value 1.
|
|
397 |
* <p>
|
|
398 |
* For example, if the first day-of-week is Sunday, then that will have the
|
|
399 |
* value 1, with other days ranging from Monday as 2 to Saturday as 7.
|
19030
|
400 |
* <p>
|
|
401 |
* In the resolving phase of parsing, a localized day-of-week will be converted
|
|
402 |
* to a standardized {@code ChronoField} day-of-week.
|
|
403 |
* The day-of-week must be in the valid range 1 to 7.
|
|
404 |
* Other fields in this class build dates using the standardized day-of-week.
|
15289
|
405 |
*
|
17474
|
406 |
* @return a field providing access to the day-of-week with localized numbering, not null
|
15289
|
407 |
*/
|
|
408 |
public TemporalField dayOfWeek() {
|
|
409 |
return dayOfWeek;
|
|
410 |
}
|
|
411 |
|
|
412 |
/**
|
17474
|
413 |
* Returns a field to access the week of month based on this {@code WeekFields}.
|
15289
|
414 |
* <p>
|
16852
|
415 |
* This represents the concept of the count of weeks within the month where weeks
|
15289
|
416 |
* start on a fixed day-of-week, such as Monday.
|
|
417 |
* This field is typically used with {@link WeekFields#dayOfWeek()}.
|
|
418 |
* <p>
|
|
419 |
* Week one (1) is the week starting on the {@link WeekFields#getFirstDayOfWeek}
|
|
420 |
* where there are at least {@link WeekFields#getMinimalDaysInFirstWeek()} days in the month.
|
|
421 |
* Thus, week one may start up to {@code minDays} days before the start of the month.
|
|
422 |
* If the first week starts after the start of the month then the period before is week zero (0).
|
|
423 |
* <p>
|
|
424 |
* For example:<br>
|
|
425 |
* - if the 1st day of the month is a Monday, week one starts on the 1st and there is no week zero<br>
|
|
426 |
* - if the 2nd day of the month is a Monday, week one starts on the 2nd and the 1st is in week zero<br>
|
|
427 |
* - if the 4th day of the month is a Monday, week one starts on the 4th and the 1st to 3rd is in week zero<br>
|
|
428 |
* - if the 5th day of the month is a Monday, week two starts on the 5th and the 1st to 4th is in week one<br>
|
|
429 |
* <p>
|
|
430 |
* This field can be used with any calendar system.
|
19030
|
431 |
* <p>
|
|
432 |
* In the resolving phase of parsing, a date can be created from a year,
|
|
433 |
* week-of-month, month-of-year and day-of-week.
|
|
434 |
* <p>
|
|
435 |
* In {@linkplain ResolverStyle#STRICT strict mode}, all four fields are
|
|
436 |
* validated against their range of valid values. The week-of-month field
|
|
437 |
* is validated to ensure that the resulting month is the month requested.
|
|
438 |
* <p>
|
|
439 |
* In {@linkplain ResolverStyle#SMART smart mode}, all four fields are
|
|
440 |
* validated against their range of valid values. The week-of-month field
|
|
441 |
* is validated from 0 to 6, meaning that the resulting date can be in a
|
|
442 |
* different month to that specified.
|
|
443 |
* <p>
|
|
444 |
* In {@linkplain ResolverStyle#LENIENT lenient mode}, the year and day-of-week
|
|
445 |
* are validated against the range of valid values. The resulting date is calculated
|
|
446 |
* equivalent to the following four stage approach.
|
|
447 |
* First, create a date on the first day of the first week of January in the requested year.
|
|
448 |
* Then take the month-of-year, subtract one, and add the amount in months to the date.
|
|
449 |
* Then take the week-of-month, subtract one, and add the amount in weeks to the date.
|
|
450 |
* Finally, adjust to the correct day-of-week within the localized week.
|
17474
|
451 |
*
|
|
452 |
* @return a field providing access to the week-of-month, not null
|
15289
|
453 |
*/
|
|
454 |
public TemporalField weekOfMonth() {
|
|
455 |
return weekOfMonth;
|
|
456 |
}
|
|
457 |
|
|
458 |
/**
|
17474
|
459 |
* Returns a field to access the week of year based on this {@code WeekFields}.
|
15289
|
460 |
* <p>
|
16852
|
461 |
* This represents the concept of the count of weeks within the year where weeks
|
15289
|
462 |
* start on a fixed day-of-week, such as Monday.
|
|
463 |
* This field is typically used with {@link WeekFields#dayOfWeek()}.
|
|
464 |
* <p>
|
|
465 |
* Week one(1) is the week starting on the {@link WeekFields#getFirstDayOfWeek}
|
16852
|
466 |
* where there are at least {@link WeekFields#getMinimalDaysInFirstWeek()} days in the year.
|
15289
|
467 |
* Thus, week one may start up to {@code minDays} days before the start of the year.
|
|
468 |
* If the first week starts after the start of the year then the period before is week zero (0).
|
|
469 |
* <p>
|
|
470 |
* For example:<br>
|
|
471 |
* - if the 1st day of the year is a Monday, week one starts on the 1st and there is no week zero<br>
|
|
472 |
* - if the 2nd day of the year is a Monday, week one starts on the 2nd and the 1st is in week zero<br>
|
|
473 |
* - if the 4th day of the year is a Monday, week one starts on the 4th and the 1st to 3rd is in week zero<br>
|
|
474 |
* - 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>
|
|
475 |
* <p>
|
|
476 |
* This field can be used with any calendar system.
|
19030
|
477 |
* <p>
|
|
478 |
* In the resolving phase of parsing, a date can be created from a year,
|
|
479 |
* week-of-year and day-of-week.
|
|
480 |
* <p>
|
|
481 |
* In {@linkplain ResolverStyle#STRICT strict mode}, all three fields are
|
|
482 |
* validated against their range of valid values. The week-of-year field
|
|
483 |
* is validated to ensure that the resulting year is the year requested.
|
|
484 |
* <p>
|
|
485 |
* In {@linkplain ResolverStyle#SMART smart mode}, all three fields are
|
|
486 |
* validated against their range of valid values. The week-of-year field
|
|
487 |
* is validated from 0 to 54, meaning that the resulting date can be in a
|
|
488 |
* different year to that specified.
|
|
489 |
* <p>
|
|
490 |
* In {@linkplain ResolverStyle#LENIENT lenient mode}, the year and day-of-week
|
|
491 |
* are validated against the range of valid values. The resulting date is calculated
|
|
492 |
* equivalent to the following three stage approach.
|
|
493 |
* First, create a date on the first day of the first week in the requested year.
|
|
494 |
* Then take the week-of-year, subtract one, and add the amount in weeks to the date.
|
|
495 |
* Finally, adjust to the correct day-of-week within the localized week.
|
17474
|
496 |
*
|
|
497 |
* @return a field providing access to the week-of-year, not null
|
15289
|
498 |
*/
|
|
499 |
public TemporalField weekOfYear() {
|
|
500 |
return weekOfYear;
|
|
501 |
}
|
|
502 |
|
|
503 |
/**
|
17474
|
504 |
* Returns a field to access the week of a week-based-year based on this {@code WeekFields}.
|
16852
|
505 |
* <p>
|
|
506 |
* This represents the concept of the count of weeks within the year where weeks
|
|
507 |
* start on a fixed day-of-week, such as Monday and each week belongs to exactly one year.
|
|
508 |
* This field is typically used with {@link WeekFields#dayOfWeek()} and
|
|
509 |
* {@link WeekFields#weekBasedYear()}.
|
|
510 |
* <p>
|
|
511 |
* Week one(1) is the week starting on the {@link WeekFields#getFirstDayOfWeek}
|
|
512 |
* where there are at least {@link WeekFields#getMinimalDaysInFirstWeek()} days in the year.
|
|
513 |
* If the first week starts after the start of the year then the period before
|
|
514 |
* is in the last week of the previous year.
|
|
515 |
* <p>
|
|
516 |
* For example:<br>
|
|
517 |
* - if the 1st day of the year is a Monday, week one starts on the 1st<br>
|
|
518 |
* - if the 2nd day of the year is a Monday, week one starts on the 2nd and
|
|
519 |
* the 1st is in the last week of the previous year<br>
|
|
520 |
* - if the 4th day of the year is a Monday, week one starts on the 4th and
|
|
521 |
* the 1st to 3rd is in the last week of the previous year<br>
|
|
522 |
* - if the 5th day of the year is a Monday, week two starts on the 5th and
|
|
523 |
* the 1st to 4th is in week one<br>
|
|
524 |
* <p>
|
|
525 |
* This field can be used with any calendar system.
|
19030
|
526 |
* <p>
|
|
527 |
* In the resolving phase of parsing, a date can be created from a week-based-year,
|
|
528 |
* week-of-year and day-of-week.
|
|
529 |
* <p>
|
|
530 |
* In {@linkplain ResolverStyle#STRICT strict mode}, all three fields are
|
|
531 |
* validated against their range of valid values. The week-of-year field
|
|
532 |
* is validated to ensure that the resulting week-based-year is the
|
|
533 |
* week-based-year requested.
|
|
534 |
* <p>
|
|
535 |
* In {@linkplain ResolverStyle#SMART smart mode}, all three fields are
|
|
536 |
* validated against their range of valid values. The week-of-week-based-year field
|
|
537 |
* is validated from 1 to 53, meaning that the resulting date can be in the
|
|
538 |
* following week-based-year to that specified.
|
|
539 |
* <p>
|
|
540 |
* In {@linkplain ResolverStyle#LENIENT lenient mode}, the year and day-of-week
|
|
541 |
* are validated against the range of valid values. The resulting date is calculated
|
|
542 |
* equivalent to the following three stage approach.
|
|
543 |
* First, create a date on the first day of the first week in the requested week-based-year.
|
|
544 |
* Then take the week-of-week-based-year, subtract one, and add the amount in weeks to the date.
|
|
545 |
* Finally, adjust to the correct day-of-week within the localized week.
|
17474
|
546 |
*
|
|
547 |
* @return a field providing access to the week-of-week-based-year, not null
|
16852
|
548 |
*/
|
|
549 |
public TemporalField weekOfWeekBasedYear() {
|
|
550 |
return weekOfWeekBasedYear;
|
|
551 |
}
|
|
552 |
|
|
553 |
/**
|
17474
|
554 |
* Returns a field to access the year of a week-based-year based on this {@code WeekFields}.
|
16852
|
555 |
* <p>
|
|
556 |
* This represents the concept of the year where weeks start on a fixed day-of-week,
|
|
557 |
* such as Monday and each week belongs to exactly one year.
|
|
558 |
* This field is typically used with {@link WeekFields#dayOfWeek()} and
|
|
559 |
* {@link WeekFields#weekOfWeekBasedYear()}.
|
|
560 |
* <p>
|
|
561 |
* Week one(1) is the week starting on the {@link WeekFields#getFirstDayOfWeek}
|
|
562 |
* where there are at least {@link WeekFields#getMinimalDaysInFirstWeek()} days in the year.
|
|
563 |
* Thus, week one may start before the start of the year.
|
|
564 |
* If the first week starts after the start of the year then the period before
|
|
565 |
* is in the last week of the previous year.
|
|
566 |
* <p>
|
|
567 |
* This field can be used with any calendar system.
|
19030
|
568 |
* <p>
|
|
569 |
* In the resolving phase of parsing, a date can be created from a week-based-year,
|
|
570 |
* week-of-year and day-of-week.
|
|
571 |
* <p>
|
|
572 |
* In {@linkplain ResolverStyle#STRICT strict mode}, all three fields are
|
|
573 |
* validated against their range of valid values. The week-of-year field
|
|
574 |
* is validated to ensure that the resulting week-based-year is the
|
|
575 |
* week-based-year requested.
|
|
576 |
* <p>
|
|
577 |
* In {@linkplain ResolverStyle#SMART smart mode}, all three fields are
|
|
578 |
* validated against their range of valid values. The week-of-week-based-year field
|
|
579 |
* is validated from 1 to 53, meaning that the resulting date can be in the
|
|
580 |
* following week-based-year to that specified.
|
|
581 |
* <p>
|
|
582 |
* In {@linkplain ResolverStyle#LENIENT lenient mode}, the year and day-of-week
|
|
583 |
* are validated against the range of valid values. The resulting date is calculated
|
|
584 |
* equivalent to the following three stage approach.
|
|
585 |
* First, create a date on the first day of the first week in the requested week-based-year.
|
|
586 |
* Then take the week-of-week-based-year, subtract one, and add the amount in weeks to the date.
|
|
587 |
* Finally, adjust to the correct day-of-week within the localized week.
|
17474
|
588 |
*
|
|
589 |
* @return a field providing access to the week-based-year, not null
|
16852
|
590 |
*/
|
|
591 |
public TemporalField weekBasedYear() {
|
|
592 |
return weekBasedYear;
|
|
593 |
}
|
|
594 |
|
17474
|
595 |
//-----------------------------------------------------------------------
|
16852
|
596 |
/**
|
17474
|
597 |
* Checks if this {@code WeekFields} is equal to the specified object.
|
15289
|
598 |
* <p>
|
|
599 |
* The comparison is based on the entire state of the rules, which is
|
|
600 |
* the first day-of-week and minimal days.
|
|
601 |
*
|
|
602 |
* @param object the other rules to compare to, null returns false
|
|
603 |
* @return true if this is equal to the specified rules
|
|
604 |
*/
|
|
605 |
@Override
|
|
606 |
public boolean equals(Object object) {
|
|
607 |
if (this == object) {
|
|
608 |
return true;
|
|
609 |
}
|
|
610 |
if (object instanceof WeekFields) {
|
|
611 |
return hashCode() == object.hashCode();
|
|
612 |
}
|
|
613 |
return false;
|
|
614 |
}
|
|
615 |
|
|
616 |
/**
|
17474
|
617 |
* A hash code for this {@code WeekFields}.
|
15289
|
618 |
*
|
|
619 |
* @return a suitable hash code
|
|
620 |
*/
|
|
621 |
@Override
|
|
622 |
public int hashCode() {
|
|
623 |
return firstDayOfWeek.ordinal() * 7 + minimalDays;
|
|
624 |
}
|
|
625 |
|
|
626 |
//-----------------------------------------------------------------------
|
|
627 |
/**
|
17474
|
628 |
* A string representation of this {@code WeekFields} instance.
|
15289
|
629 |
*
|
|
630 |
* @return the string representation, not null
|
|
631 |
*/
|
|
632 |
@Override
|
|
633 |
public String toString() {
|
|
634 |
return "WeekFields[" + firstDayOfWeek + ',' + minimalDays + ']';
|
|
635 |
}
|
|
636 |
|
|
637 |
//-----------------------------------------------------------------------
|
|
638 |
/**
|
|
639 |
* Field type that computes DayOfWeek, WeekOfMonth, and WeekOfYear
|
|
640 |
* based on a WeekFields.
|
|
641 |
* A separate Field instance is required for each different WeekFields;
|
|
642 |
* combination of start of week and minimum number of days.
|
|
643 |
* Constructors are provided to create fields for DayOfWeek, WeekOfMonth,
|
|
644 |
* and WeekOfYear.
|
|
645 |
*/
|
|
646 |
static class ComputedDayOfField implements TemporalField {
|
|
647 |
|
|
648 |
/**
|
|
649 |
* Returns a field to access the day of week,
|
|
650 |
* computed based on a WeekFields.
|
|
651 |
* <p>
|
|
652 |
* The WeekDefintion of the first day of the week is used with
|
|
653 |
* the ISO DAY_OF_WEEK field to compute week boundaries.
|
|
654 |
*/
|
|
655 |
static ComputedDayOfField ofDayOfWeekField(WeekFields weekDef) {
|
15658
|
656 |
return new ComputedDayOfField("DayOfWeek", weekDef, DAYS, WEEKS, DAY_OF_WEEK_RANGE);
|
15289
|
657 |
}
|
|
658 |
|
|
659 |
/**
|
|
660 |
* Returns a field to access the week of month,
|
|
661 |
* computed based on a WeekFields.
|
|
662 |
* @see WeekFields#weekOfMonth()
|
|
663 |
*/
|
|
664 |
static ComputedDayOfField ofWeekOfMonthField(WeekFields weekDef) {
|
15658
|
665 |
return new ComputedDayOfField("WeekOfMonth", weekDef, WEEKS, MONTHS, WEEK_OF_MONTH_RANGE);
|
15289
|
666 |
}
|
|
667 |
|
|
668 |
/**
|
|
669 |
* Returns a field to access the week of year,
|
|
670 |
* computed based on a WeekFields.
|
|
671 |
* @see WeekFields#weekOfYear()
|
|
672 |
*/
|
|
673 |
static ComputedDayOfField ofWeekOfYearField(WeekFields weekDef) {
|
15658
|
674 |
return new ComputedDayOfField("WeekOfYear", weekDef, WEEKS, YEARS, WEEK_OF_YEAR_RANGE);
|
15289
|
675 |
}
|
16852
|
676 |
|
|
677 |
/**
|
|
678 |
* Returns a field to access the week of week-based-year,
|
|
679 |
* computed based on a WeekFields.
|
|
680 |
* @see WeekFields#weekOfWeekBasedYear()
|
|
681 |
*/
|
|
682 |
static ComputedDayOfField ofWeekOfWeekBasedYearField(WeekFields weekDef) {
|
|
683 |
return new ComputedDayOfField("WeekOfWeekBasedYear", weekDef, WEEKS, IsoFields.WEEK_BASED_YEARS, WEEK_OF_YEAR_RANGE);
|
|
684 |
}
|
|
685 |
|
|
686 |
/**
|
|
687 |
* Returns a field to access the week of week-based-year,
|
|
688 |
* computed based on a WeekFields.
|
|
689 |
* @see WeekFields#weekBasedYear()
|
|
690 |
*/
|
|
691 |
static ComputedDayOfField ofWeekBasedYearField(WeekFields weekDef) {
|
|
692 |
return new ComputedDayOfField("WeekBasedYear", weekDef, IsoFields.WEEK_BASED_YEARS, FOREVER, ChronoField.YEAR.range());
|
|
693 |
}
|
|
694 |
|
|
695 |
/**
|
|
696 |
* Return a new week-based-year date of the Chronology, year, week-of-year,
|
|
697 |
* and dow of week.
|
|
698 |
* @param chrono The chronology of the new date
|
|
699 |
* @param yowby the year of the week-based-year
|
|
700 |
* @param wowby the week of the week-based-year
|
|
701 |
* @param dow the day of the week
|
|
702 |
* @return a ChronoLocalDate for the requested year, week of year, and day of week
|
|
703 |
*/
|
19030
|
704 |
private ChronoLocalDate ofWeekBasedYear(Chronology chrono,
|
16852
|
705 |
int yowby, int wowby, int dow) {
|
19030
|
706 |
ChronoLocalDate date = chrono.date(yowby, 1, 1);
|
16852
|
707 |
int ldow = localizedDayOfWeek(date);
|
|
708 |
int offset = startOfWeekOffset(1, ldow);
|
|
709 |
|
|
710 |
// Clamp the week of year to keep it in the same year
|
|
711 |
int yearLen = date.lengthOfYear();
|
|
712 |
int newYearWeek = computeWeek(offset, yearLen + weekDef.getMinimalDaysInFirstWeek());
|
|
713 |
wowby = Math.min(wowby, newYearWeek - 1);
|
|
714 |
|
|
715 |
int days = -offset + (dow - 1) + (wowby - 1) * 7;
|
|
716 |
return date.plus(days, DAYS);
|
|
717 |
}
|
|
718 |
|
15289
|
719 |
private final String name;
|
|
720 |
private final WeekFields weekDef;
|
|
721 |
private final TemporalUnit baseUnit;
|
|
722 |
private final TemporalUnit rangeUnit;
|
|
723 |
private final ValueRange range;
|
|
724 |
|
|
725 |
private ComputedDayOfField(String name, WeekFields weekDef, TemporalUnit baseUnit, TemporalUnit rangeUnit, ValueRange range) {
|
|
726 |
this.name = name;
|
|
727 |
this.weekDef = weekDef;
|
|
728 |
this.baseUnit = baseUnit;
|
|
729 |
this.rangeUnit = rangeUnit;
|
|
730 |
this.range = range;
|
|
731 |
}
|
|
732 |
|
|
733 |
private static final ValueRange DAY_OF_WEEK_RANGE = ValueRange.of(1, 7);
|
15658
|
734 |
private static final ValueRange WEEK_OF_MONTH_RANGE = ValueRange.of(0, 1, 4, 6);
|
|
735 |
private static final ValueRange WEEK_OF_YEAR_RANGE = ValueRange.of(0, 1, 52, 54);
|
15289
|
736 |
|
|
737 |
@Override
|
15658
|
738 |
public long getFrom(TemporalAccessor temporal) {
|
|
739 |
if (rangeUnit == WEEKS) { // day-of-week
|
16852
|
740 |
return localizedDayOfWeek(temporal);
|
15658
|
741 |
} else if (rangeUnit == MONTHS) { // week-of-month
|
16852
|
742 |
return localizedWeekOfMonth(temporal);
|
15658
|
743 |
} else if (rangeUnit == YEARS) { // week-of-year
|
16852
|
744 |
return localizedWeekOfYear(temporal);
|
|
745 |
} else if (rangeUnit == WEEK_BASED_YEARS) {
|
|
746 |
return localizedWeekOfWeekBasedYear(temporal);
|
|
747 |
} else if (rangeUnit == FOREVER) {
|
|
748 |
return localizedWeekBasedYear(temporal);
|
15289
|
749 |
} else {
|
16852
|
750 |
throw new IllegalStateException("unreachable, rangeUnit: " + rangeUnit + ", this: " + this);
|
15289
|
751 |
}
|
|
752 |
}
|
|
753 |
|
16852
|
754 |
private int localizedDayOfWeek(TemporalAccessor temporal) {
|
|
755 |
int sow = weekDef.getFirstDayOfWeek().getValue();
|
15658
|
756 |
int isoDow = temporal.get(DAY_OF_WEEK);
|
|
757 |
return Math.floorMod(isoDow - sow, 7) + 1;
|
|
758 |
}
|
|
759 |
|
19030
|
760 |
private int localizedDayOfWeek(int isoDow) {
|
|
761 |
int sow = weekDef.getFirstDayOfWeek().getValue();
|
|
762 |
return Math.floorMod(isoDow - sow, 7) + 1;
|
|
763 |
}
|
|
764 |
|
16852
|
765 |
private long localizedWeekOfMonth(TemporalAccessor temporal) {
|
|
766 |
int dow = localizedDayOfWeek(temporal);
|
15658
|
767 |
int dom = temporal.get(DAY_OF_MONTH);
|
|
768 |
int offset = startOfWeekOffset(dom, dow);
|
|
769 |
return computeWeek(offset, dom);
|
|
770 |
}
|
|
771 |
|
16852
|
772 |
private long localizedWeekOfYear(TemporalAccessor temporal) {
|
|
773 |
int dow = localizedDayOfWeek(temporal);
|
15658
|
774 |
int doy = temporal.get(DAY_OF_YEAR);
|
|
775 |
int offset = startOfWeekOffset(doy, dow);
|
|
776 |
return computeWeek(offset, doy);
|
|
777 |
}
|
|
778 |
|
15289
|
779 |
/**
|
16852
|
780 |
* Returns the year of week-based-year for the temporal.
|
|
781 |
* The year can be the previous year, the current year, or the next year.
|
|
782 |
* @param temporal a date of any chronology, not null
|
|
783 |
* @return the year of week-based-year for the date
|
|
784 |
*/
|
|
785 |
private int localizedWeekBasedYear(TemporalAccessor temporal) {
|
|
786 |
int dow = localizedDayOfWeek(temporal);
|
|
787 |
int year = temporal.get(YEAR);
|
|
788 |
int doy = temporal.get(DAY_OF_YEAR);
|
|
789 |
int offset = startOfWeekOffset(doy, dow);
|
|
790 |
int week = computeWeek(offset, doy);
|
|
791 |
if (week == 0) {
|
|
792 |
// Day is in end of week of previous year; return the previous year
|
|
793 |
return year - 1;
|
|
794 |
} else {
|
|
795 |
// If getting close to end of year, use higher precision logic
|
|
796 |
// Check if date of year is in partial week associated with next year
|
|
797 |
ValueRange dayRange = temporal.range(DAY_OF_YEAR);
|
|
798 |
int yearLen = (int)dayRange.getMaximum();
|
|
799 |
int newYearWeek = computeWeek(offset, yearLen + weekDef.getMinimalDaysInFirstWeek());
|
|
800 |
if (week >= newYearWeek) {
|
|
801 |
return year + 1;
|
|
802 |
}
|
|
803 |
}
|
|
804 |
return year;
|
|
805 |
}
|
|
806 |
|
|
807 |
/**
|
|
808 |
* Returns the week of week-based-year for the temporal.
|
|
809 |
* The week can be part of the previous year, the current year,
|
|
810 |
* or the next year depending on the week start and minimum number
|
|
811 |
* of days.
|
|
812 |
* @param temporal a date of any chronology
|
|
813 |
* @return the week of the year
|
|
814 |
* @see #localizedWeekBasedYear(java.time.temporal.TemporalAccessor)
|
|
815 |
*/
|
|
816 |
private int localizedWeekOfWeekBasedYear(TemporalAccessor temporal) {
|
|
817 |
int dow = localizedDayOfWeek(temporal);
|
|
818 |
int doy = temporal.get(DAY_OF_YEAR);
|
|
819 |
int offset = startOfWeekOffset(doy, dow);
|
|
820 |
int week = computeWeek(offset, doy);
|
|
821 |
if (week == 0) {
|
|
822 |
// Day is in end of week of previous year
|
|
823 |
// Recompute from the last day of the previous year
|
|
824 |
ChronoLocalDate date = Chronology.from(temporal).date(temporal);
|
|
825 |
date = date.minus(doy, DAYS); // Back down into previous year
|
|
826 |
return localizedWeekOfWeekBasedYear(date);
|
|
827 |
} else if (week > 50) {
|
|
828 |
// If getting close to end of year, use higher precision logic
|
|
829 |
// Check if date of year is in partial week associated with next year
|
|
830 |
ValueRange dayRange = temporal.range(DAY_OF_YEAR);
|
|
831 |
int yearLen = (int)dayRange.getMaximum();
|
|
832 |
int newYearWeek = computeWeek(offset, yearLen + weekDef.getMinimalDaysInFirstWeek());
|
|
833 |
if (week >= newYearWeek) {
|
|
834 |
// Overlaps with week of following year; reduce to week in following year
|
|
835 |
week = week - newYearWeek + 1;
|
|
836 |
}
|
|
837 |
}
|
|
838 |
return week;
|
|
839 |
}
|
|
840 |
|
|
841 |
/**
|
15289
|
842 |
* Returns an offset to align week start with a day of month or day of year.
|
|
843 |
*
|
16852
|
844 |
* @param day the day; 1 through infinity
|
|
845 |
* @param dow the day of the week of that day; 1 through 7
|
|
846 |
* @return an offset in days to align a day with the start of the first 'full' week
|
15289
|
847 |
*/
|
|
848 |
private int startOfWeekOffset(int day, int dow) {
|
|
849 |
// offset of first day corresponding to the day of week in first 7 days (zero origin)
|
|
850 |
int weekStart = Math.floorMod(day - dow, 7);
|
|
851 |
int offset = -weekStart;
|
|
852 |
if (weekStart + 1 > weekDef.getMinimalDaysInFirstWeek()) {
|
|
853 |
// The previous week has the minimum days in the current month to be a 'week'
|
|
854 |
offset = 7 - weekStart;
|
|
855 |
}
|
|
856 |
return offset;
|
|
857 |
}
|
|
858 |
|
|
859 |
/**
|
|
860 |
* Returns the week number computed from the reference day and reference dayOfWeek.
|
|
861 |
*
|
|
862 |
* @param offset the offset to align a date with the start of week
|
|
863 |
* from {@link #startOfWeekOffset}.
|
|
864 |
* @param day the day for which to compute the week number
|
|
865 |
* @return the week number where zero is used for a partial week and 1 for the first full week
|
|
866 |
*/
|
|
867 |
private int computeWeek(int offset, int day) {
|
|
868 |
return ((7 + offset + (day - 1)) / 7);
|
|
869 |
}
|
|
870 |
|
15658
|
871 |
@SuppressWarnings("unchecked")
|
15289
|
872 |
@Override
|
15658
|
873 |
public <R extends Temporal> R adjustInto(R temporal, long newValue) {
|
15289
|
874 |
// Check the new value and get the old value of the field
|
15658
|
875 |
int newVal = range.checkValidIntValue(newValue, this); // lenient check range
|
15289
|
876 |
int currentVal = temporal.get(this);
|
|
877 |
if (newVal == currentVal) {
|
|
878 |
return temporal;
|
|
879 |
}
|
16852
|
880 |
|
|
881 |
if (rangeUnit == FOREVER) { // replace year of WeekBasedYear
|
|
882 |
// Create a new date object with the same chronology,
|
|
883 |
// the desired year and the same week and dow.
|
|
884 |
int idow = temporal.get(weekDef.dayOfWeek);
|
|
885 |
int wowby = temporal.get(weekDef.weekOfWeekBasedYear);
|
|
886 |
return (R) ofWeekBasedYear(Chronology.from(temporal), (int)newValue, wowby, idow);
|
|
887 |
} else {
|
|
888 |
// Compute the difference and add that using the base unit of the field
|
|
889 |
return (R) temporal.plus(newVal - currentVal, baseUnit);
|
|
890 |
}
|
15289
|
891 |
}
|
|
892 |
|
|
893 |
@Override
|
19030
|
894 |
public ChronoLocalDate resolve(
|
|
895 |
Map<TemporalField, Long> fieldValues, Chronology chronology, ZoneId zone, ResolverStyle resolverStyle) {
|
|
896 |
final long value = fieldValues.get(this);
|
|
897 |
final int newValue = Math.toIntExact(value); // broad limit makes overflow checking lighter
|
|
898 |
// first convert localized day-of-week to ISO day-of-week
|
|
899 |
// doing this first handles case where both ISO and localized were parsed and might mismatch
|
|
900 |
// day-of-week is always strict as two different day-of-week values makes lenient complex
|
15658
|
901 |
if (rangeUnit == WEEKS) { // day-of-week
|
19030
|
902 |
final int checkedValue = range.checkValidIntValue(value, this); // no leniency as too complex
|
|
903 |
final int startDow = weekDef.getFirstDayOfWeek().getValue();
|
|
904 |
long isoDow = Math.floorMod((startDow - 1) + (checkedValue - 1), 7) + 1;
|
|
905 |
fieldValues.remove(this);
|
|
906 |
fieldValues.put(DAY_OF_WEEK, isoDow);
|
15658
|
907 |
return null;
|
|
908 |
}
|
19030
|
909 |
|
|
910 |
// can only build date if ISO day-of-week is present
|
|
911 |
if (fieldValues.containsKey(DAY_OF_WEEK) == false) {
|
|
912 |
return null;
|
|
913 |
}
|
|
914 |
int isoDow = DAY_OF_WEEK.checkValidIntValue(fieldValues.get(DAY_OF_WEEK));
|
|
915 |
int dow = localizedDayOfWeek(isoDow);
|
|
916 |
|
|
917 |
// build date
|
|
918 |
if (fieldValues.containsKey(YEAR)) {
|
|
919 |
int year = YEAR.checkValidIntValue(fieldValues.get(YEAR)); // validate
|
|
920 |
if (rangeUnit == MONTHS && fieldValues.containsKey(MONTH_OF_YEAR)) { // week-of-month
|
|
921 |
long month = fieldValues.get(MONTH_OF_YEAR); // not validated yet
|
|
922 |
return resolveWoM(fieldValues, chronology, year, month, newValue, dow, resolverStyle);
|
15658
|
923 |
}
|
19030
|
924 |
if (rangeUnit == YEARS) { // week-of-year
|
|
925 |
return resolveWoY(fieldValues, chronology, year, newValue, dow, resolverStyle);
|
16852
|
926 |
}
|
19030
|
927 |
} else if ((rangeUnit == WEEK_BASED_YEARS || rangeUnit == FOREVER) &&
|
|
928 |
fieldValues.containsKey(weekDef.weekBasedYear) &&
|
|
929 |
fieldValues.containsKey(weekDef.weekOfWeekBasedYear)) { // week-of-week-based-year and year-of-week-based-year
|
|
930 |
return resolveWBY(fieldValues, chronology, dow, resolverStyle);
|
15289
|
931 |
}
|
16852
|
932 |
return null;
|
15289
|
933 |
}
|
|
934 |
|
19030
|
935 |
private ChronoLocalDate resolveWoM(
|
|
936 |
Map<TemporalField, Long> fieldValues, Chronology chrono, int year, long month, long wom, int localDow, ResolverStyle resolverStyle) {
|
|
937 |
ChronoLocalDate date;
|
|
938 |
if (resolverStyle == ResolverStyle.LENIENT) {
|
|
939 |
date = chrono.date(year, 1, 1).plus(Math.subtractExact(month, 1), MONTHS);
|
|
940 |
long weeks = Math.subtractExact(wom, localizedWeekOfMonth(date));
|
|
941 |
int days = localDow - localizedDayOfWeek(date); // safe from overflow
|
|
942 |
date = date.plus(Math.addExact(Math.multiplyExact(weeks, 7), days), DAYS);
|
|
943 |
} else {
|
|
944 |
int monthValid = MONTH_OF_YEAR.checkValidIntValue(month); // validate
|
|
945 |
date = chrono.date(year, monthValid, 1);
|
|
946 |
int womInt = range.checkValidIntValue(wom, this); // validate
|
|
947 |
int weeks = (int) (womInt - localizedWeekOfMonth(date)); // safe from overflow
|
|
948 |
int days = localDow - localizedDayOfWeek(date); // safe from overflow
|
|
949 |
date = date.plus(weeks * 7 + days, DAYS);
|
|
950 |
if (resolverStyle == ResolverStyle.STRICT && date.getLong(MONTH_OF_YEAR) != month) {
|
|
951 |
throw new DateTimeException("Strict mode rejected resolved date as it is in a different month");
|
|
952 |
}
|
|
953 |
}
|
|
954 |
fieldValues.remove(this);
|
|
955 |
fieldValues.remove(YEAR);
|
|
956 |
fieldValues.remove(MONTH_OF_YEAR);
|
|
957 |
fieldValues.remove(DAY_OF_WEEK);
|
|
958 |
return date;
|
15289
|
959 |
}
|
|
960 |
|
19030
|
961 |
private ChronoLocalDate resolveWoY(
|
|
962 |
Map<TemporalField, Long> fieldValues, Chronology chrono, int year, long woy, int localDow, ResolverStyle resolverStyle) {
|
|
963 |
ChronoLocalDate date = chrono.date(year, 1, 1);
|
|
964 |
if (resolverStyle == ResolverStyle.LENIENT) {
|
|
965 |
long weeks = Math.subtractExact(woy, localizedWeekOfYear(date));
|
|
966 |
int days = localDow - localizedDayOfWeek(date); // safe from overflow
|
|
967 |
date = date.plus(Math.addExact(Math.multiplyExact(weeks, 7), days), DAYS);
|
|
968 |
} else {
|
|
969 |
int womInt = range.checkValidIntValue(woy, this); // validate
|
|
970 |
int weeks = (int) (womInt - localizedWeekOfYear(date)); // safe from overflow
|
|
971 |
int days = localDow - localizedDayOfWeek(date); // safe from overflow
|
|
972 |
date = date.plus(weeks * 7 + days, DAYS);
|
|
973 |
if (resolverStyle == ResolverStyle.STRICT && date.getLong(YEAR) != year) {
|
|
974 |
throw new DateTimeException("Strict mode rejected resolved date as it is in a different year");
|
|
975 |
}
|
|
976 |
}
|
|
977 |
fieldValues.remove(this);
|
|
978 |
fieldValues.remove(YEAR);
|
|
979 |
fieldValues.remove(DAY_OF_WEEK);
|
|
980 |
return date;
|
|
981 |
}
|
|
982 |
|
|
983 |
private ChronoLocalDate resolveWBY(
|
|
984 |
Map<TemporalField, Long> fieldValues, Chronology chrono, int localDow, ResolverStyle resolverStyle) {
|
|
985 |
int yowby = weekDef.weekBasedYear.range().checkValidIntValue(
|
|
986 |
fieldValues.get(weekDef.weekBasedYear), weekDef.weekBasedYear);
|
|
987 |
ChronoLocalDate date;
|
|
988 |
if (resolverStyle == ResolverStyle.LENIENT) {
|
|
989 |
date = ofWeekBasedYear(chrono, yowby, 1, localDow);
|
|
990 |
long wowby = fieldValues.get(weekDef.weekOfWeekBasedYear);
|
|
991 |
long weeks = Math.subtractExact(wowby, 1);
|
|
992 |
date = date.plus(weeks, WEEKS);
|
|
993 |
} else {
|
|
994 |
int wowby = weekDef.weekOfWeekBasedYear.range().checkValidIntValue(
|
|
995 |
fieldValues.get(weekDef.weekOfWeekBasedYear), weekDef.weekOfWeekBasedYear); // validate
|
|
996 |
date = ofWeekBasedYear(chrono, yowby, wowby, localDow);
|
|
997 |
if (resolverStyle == ResolverStyle.STRICT && localizedWeekBasedYear(date) != yowby) {
|
|
998 |
throw new DateTimeException("Strict mode rejected resolved date as it is in a different week-based-year");
|
|
999 |
}
|
|
1000 |
}
|
|
1001 |
fieldValues.remove(this);
|
|
1002 |
fieldValues.remove(weekDef.weekBasedYear);
|
|
1003 |
fieldValues.remove(weekDef.weekOfWeekBasedYear);
|
|
1004 |
fieldValues.remove(DAY_OF_WEEK);
|
|
1005 |
return date;
|
|
1006 |
}
|
|
1007 |
|
|
1008 |
//-----------------------------------------------------------------------
|
15289
|
1009 |
@Override
|
16852
|
1010 |
public String getDisplayName(Locale locale) {
|
|
1011 |
Objects.requireNonNull(locale, "locale");
|
|
1012 |
if (rangeUnit == YEARS) { // only have values for week-of-year
|
|
1013 |
LocaleResources lr = LocaleProviderAdapter.getResourceBundleBased()
|
|
1014 |
.getLocaleResources(locale);
|
|
1015 |
ResourceBundle rb = lr.getJavaTimeFormatData();
|
19030
|
1016 |
return rb.containsKey("field.week") ? rb.getString("field.week") : name;
|
16852
|
1017 |
}
|
19030
|
1018 |
return name;
|
16852
|
1019 |
}
|
|
1020 |
|
|
1021 |
@Override
|
15289
|
1022 |
public TemporalUnit getBaseUnit() {
|
|
1023 |
return baseUnit;
|
|
1024 |
}
|
|
1025 |
|
|
1026 |
@Override
|
|
1027 |
public TemporalUnit getRangeUnit() {
|
|
1028 |
return rangeUnit;
|
|
1029 |
}
|
|
1030 |
|
|
1031 |
@Override
|
16852
|
1032 |
public boolean isDateBased() {
|
|
1033 |
return true;
|
|
1034 |
}
|
|
1035 |
|
|
1036 |
@Override
|
19030
|
1037 |
public boolean isTimeBased() {
|
|
1038 |
return false;
|
|
1039 |
}
|
|
1040 |
|
|
1041 |
@Override
|
15289
|
1042 |
public ValueRange range() {
|
|
1043 |
return range;
|
|
1044 |
}
|
|
1045 |
|
|
1046 |
//-----------------------------------------------------------------------
|
|
1047 |
@Override
|
15658
|
1048 |
public boolean isSupportedBy(TemporalAccessor temporal) {
|
|
1049 |
if (temporal.isSupported(DAY_OF_WEEK)) {
|
|
1050 |
if (rangeUnit == WEEKS) { // day-of-week
|
15289
|
1051 |
return true;
|
15658
|
1052 |
} else if (rangeUnit == MONTHS) { // week-of-month
|
|
1053 |
return temporal.isSupported(DAY_OF_MONTH);
|
|
1054 |
} else if (rangeUnit == YEARS) { // week-of-year
|
|
1055 |
return temporal.isSupported(DAY_OF_YEAR);
|
16852
|
1056 |
} else if (rangeUnit == WEEK_BASED_YEARS) {
|
|
1057 |
return temporal.isSupported(DAY_OF_YEAR);
|
|
1058 |
} else if (rangeUnit == FOREVER) {
|
|
1059 |
return temporal.isSupported(YEAR);
|
15289
|
1060 |
}
|
|
1061 |
}
|
|
1062 |
return false;
|
|
1063 |
}
|
|
1064 |
|
|
1065 |
@Override
|
15658
|
1066 |
public ValueRange rangeRefinedBy(TemporalAccessor temporal) {
|
|
1067 |
if (rangeUnit == ChronoUnit.WEEKS) { // day-of-week
|
15289
|
1068 |
return range;
|
16852
|
1069 |
} else if (rangeUnit == MONTHS) { // week-of-month
|
|
1070 |
return rangeByWeek(temporal, DAY_OF_MONTH);
|
15658
|
1071 |
} else if (rangeUnit == YEARS) { // week-of-year
|
16852
|
1072 |
return rangeByWeek(temporal, DAY_OF_YEAR);
|
|
1073 |
} else if (rangeUnit == WEEK_BASED_YEARS) {
|
|
1074 |
return rangeWeekOfWeekBasedYear(temporal);
|
|
1075 |
} else if (rangeUnit == FOREVER) {
|
|
1076 |
return YEAR.range();
|
15289
|
1077 |
} else {
|
16852
|
1078 |
throw new IllegalStateException("unreachable, rangeUnit: " + rangeUnit + ", this: " + this);
|
15289
|
1079 |
}
|
16852
|
1080 |
}
|
15289
|
1081 |
|
16852
|
1082 |
/**
|
|
1083 |
* Map the field range to a week range
|
|
1084 |
* @param temporal the temporal
|
|
1085 |
* @param field the field to get the range of
|
|
1086 |
* @return the ValueRange with the range adjusted to weeks.
|
|
1087 |
*/
|
|
1088 |
private ValueRange rangeByWeek(TemporalAccessor temporal, TemporalField field) {
|
|
1089 |
int dow = localizedDayOfWeek(temporal);
|
15289
|
1090 |
int offset = startOfWeekOffset(temporal.get(field), dow);
|
|
1091 |
ValueRange fieldRange = temporal.range(field);
|
|
1092 |
return ValueRange.of(computeWeek(offset, (int) fieldRange.getMinimum()),
|
|
1093 |
computeWeek(offset, (int) fieldRange.getMaximum()));
|
|
1094 |
}
|
|
1095 |
|
16852
|
1096 |
/**
|
|
1097 |
* Map the field range to a week range of a week year.
|
|
1098 |
* @param temporal the temporal
|
|
1099 |
* @return the ValueRange with the range adjusted to weeks.
|
|
1100 |
*/
|
|
1101 |
private ValueRange rangeWeekOfWeekBasedYear(TemporalAccessor temporal) {
|
|
1102 |
if (!temporal.isSupported(DAY_OF_YEAR)) {
|
|
1103 |
return WEEK_OF_YEAR_RANGE;
|
|
1104 |
}
|
|
1105 |
int dow = localizedDayOfWeek(temporal);
|
|
1106 |
int doy = temporal.get(DAY_OF_YEAR);
|
|
1107 |
int offset = startOfWeekOffset(doy, dow);
|
|
1108 |
int week = computeWeek(offset, doy);
|
|
1109 |
if (week == 0) {
|
|
1110 |
// Day is in end of week of previous year
|
|
1111 |
// Recompute from the last day of the previous year
|
|
1112 |
ChronoLocalDate date = Chronology.from(temporal).date(temporal);
|
|
1113 |
date = date.minus(doy + 7, DAYS); // Back down into previous year
|
|
1114 |
return rangeWeekOfWeekBasedYear(date);
|
|
1115 |
}
|
|
1116 |
// Check if day of year is in partial week associated with next year
|
|
1117 |
ValueRange dayRange = temporal.range(DAY_OF_YEAR);
|
|
1118 |
int yearLen = (int)dayRange.getMaximum();
|
|
1119 |
int newYearWeek = computeWeek(offset, yearLen + weekDef.getMinimalDaysInFirstWeek());
|
|
1120 |
|
|
1121 |
if (week >= newYearWeek) {
|
|
1122 |
// Overlaps with weeks of following year; recompute from a week in following year
|
|
1123 |
ChronoLocalDate date = Chronology.from(temporal).date(temporal);
|
|
1124 |
date = date.plus(yearLen - doy + 1 + 7, ChronoUnit.DAYS);
|
|
1125 |
return rangeWeekOfWeekBasedYear(date);
|
|
1126 |
}
|
|
1127 |
return ValueRange.of(1, newYearWeek-1);
|
|
1128 |
}
|
|
1129 |
|
15289
|
1130 |
//-----------------------------------------------------------------------
|
|
1131 |
@Override
|
|
1132 |
public String toString() {
|
19030
|
1133 |
return name + "[" + weekDef.toString() + "]";
|
15289
|
1134 |
}
|
|
1135 |
}
|
|
1136 |
}
|