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