15658
|
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 |
* Copyright (c) 2012, Stephen Colebourne & Michael Nascimento Santos
|
|
28 |
*
|
|
29 |
* All rights reserved.
|
|
30 |
*
|
|
31 |
* Redistribution and use in source and binary forms, with or without
|
|
32 |
* modification, are permitted provided that the following conditions are met:
|
|
33 |
*
|
|
34 |
* * Redistributions of source code must retain the above copyright notice,
|
|
35 |
* this list of conditions and the following disclaimer.
|
|
36 |
*
|
|
37 |
* * Redistributions in binary form must reproduce the above copyright notice,
|
|
38 |
* this list of conditions and the following disclaimer in the documentation
|
|
39 |
* and/or other materials provided with the distribution.
|
|
40 |
*
|
|
41 |
* * Neither the name of JSR-310 nor the names of its contributors
|
|
42 |
* may be used to endorse or promote products derived from this software
|
|
43 |
* without specific prior written permission.
|
|
44 |
*
|
|
45 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
46 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
47 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
48 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
49 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
50 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
51 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
52 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
53 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
54 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
55 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
56 |
*/
|
|
57 |
package java.time.chrono;
|
|
58 |
|
|
59 |
import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH;
|
|
60 |
import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR;
|
|
61 |
import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_MONTH;
|
|
62 |
import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR;
|
|
63 |
import static java.time.temporal.ChronoField.DAY_OF_MONTH;
|
|
64 |
import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
|
|
65 |
import static java.time.temporal.ChronoField.YEAR;
|
|
66 |
|
|
67 |
import java.io.IOException;
|
|
68 |
import java.io.ObjectInput;
|
|
69 |
import java.io.ObjectOutput;
|
|
70 |
import java.io.Serializable;
|
|
71 |
import java.time.Clock;
|
|
72 |
import java.time.DateTimeException;
|
|
73 |
import java.time.LocalDate;
|
|
74 |
import java.time.LocalTime;
|
|
75 |
import java.time.Period;
|
|
76 |
import java.time.ZoneId;
|
|
77 |
import java.time.temporal.ChronoField;
|
|
78 |
import java.time.temporal.TemporalAccessor;
|
|
79 |
import java.time.temporal.TemporalAdjuster;
|
|
80 |
import java.time.temporal.TemporalAmount;
|
|
81 |
import java.time.temporal.TemporalField;
|
16852
|
82 |
import java.time.temporal.TemporalQuery;
|
15658
|
83 |
import java.time.temporal.TemporalUnit;
|
16852
|
84 |
import java.time.temporal.UnsupportedTemporalTypeException;
|
15658
|
85 |
import java.time.temporal.ValueRange;
|
|
86 |
|
|
87 |
/**
|
|
88 |
* A date in the Hijrah calendar system.
|
|
89 |
* <p>
|
16852
|
90 |
* This date operates using one of several variants of the
|
|
91 |
* {@linkplain HijrahChronology Hijrah calendar}.
|
15658
|
92 |
* <p>
|
|
93 |
* The Hijrah calendar has a different total of days in a year than
|
16852
|
94 |
* Gregorian calendar, and the length of each month is based on the period
|
|
95 |
* of a complete revolution of the moon around the earth
|
|
96 |
* (as between successive new moons).
|
|
97 |
* Refer to the {@link HijrahChronology} for details of supported variants.
|
15658
|
98 |
* <p>
|
16852
|
99 |
* Each HijrahDate is created bound to a particular HijrahChronology,
|
|
100 |
* The same chronology is propagated to each HijrahDate computed from the date.
|
|
101 |
* To use a different Hijrah variant, its HijrahChronology can be used
|
|
102 |
* to create new HijrahDate instances.
|
|
103 |
* Alternatively, the {@link #withVariant} method can be used to convert
|
|
104 |
* to a new HijrahChronology.
|
17474
|
105 |
* @implSpec
|
15658
|
106 |
* This class is immutable and thread-safe.
|
|
107 |
*
|
|
108 |
* @since 1.8
|
|
109 |
*/
|
|
110 |
public final class HijrahDate
|
|
111 |
extends ChronoDateImpl<HijrahDate>
|
19030
|
112 |
implements ChronoLocalDate, Serializable {
|
15658
|
113 |
|
|
114 |
/**
|
|
115 |
* Serialization version.
|
|
116 |
*/
|
|
117 |
private static final long serialVersionUID = -5207853542612002020L;
|
|
118 |
/**
|
|
119 |
* The Chronology of this HijrahDate.
|
|
120 |
*/
|
|
121 |
private final HijrahChronology chrono;
|
|
122 |
/**
|
16852
|
123 |
* The proleptic year.
|
15658
|
124 |
*/
|
16852
|
125 |
private final transient int prolepticYear;
|
15658
|
126 |
/**
|
|
127 |
* The month-of-year.
|
|
128 |
*/
|
|
129 |
private final transient int monthOfYear;
|
|
130 |
/**
|
|
131 |
* The day-of-month.
|
|
132 |
*/
|
|
133 |
private final transient int dayOfMonth;
|
|
134 |
|
|
135 |
//-------------------------------------------------------------------------
|
|
136 |
/**
|
16852
|
137 |
* Obtains an instance of {@code HijrahDate} from the Hijrah proleptic year,
|
|
138 |
* month-of-year and day-of-month.
|
15658
|
139 |
*
|
16852
|
140 |
* @param prolepticYear the proleptic year to represent in the Hijrah calendar
|
15658
|
141 |
* @param monthOfYear the month-of-year to represent, from 1 to 12
|
|
142 |
* @param dayOfMonth the day-of-month to represent, from 1 to 30
|
|
143 |
* @return the Hijrah date, never null
|
|
144 |
* @throws DateTimeException if the value of any field is out of range
|
|
145 |
*/
|
|
146 |
static HijrahDate of(HijrahChronology chrono, int prolepticYear, int monthOfYear, int dayOfMonth) {
|
16852
|
147 |
return new HijrahDate(chrono, prolepticYear, monthOfYear, dayOfMonth);
|
15658
|
148 |
}
|
|
149 |
|
|
150 |
/**
|
16852
|
151 |
* Returns a HijrahDate for the chronology and epochDay.
|
|
152 |
* @param chrono The Hijrah chronology
|
|
153 |
* @param epochDay the epoch day
|
|
154 |
* @return a HijrahDate for the epoch day; non-null
|
15658
|
155 |
*/
|
|
156 |
static HijrahDate ofEpochDay(HijrahChronology chrono, long epochDay) {
|
|
157 |
return new HijrahDate(chrono, epochDay);
|
|
158 |
}
|
|
159 |
|
|
160 |
//-----------------------------------------------------------------------
|
|
161 |
/**
|
16852
|
162 |
* Obtains the current {@code HijrahDate} of the Islamic Umm Al-Qura calendar
|
|
163 |
* in the default time-zone.
|
15658
|
164 |
* <p>
|
|
165 |
* This will query the {@link Clock#systemDefaultZone() system clock} in the default
|
|
166 |
* time-zone to obtain the current date.
|
|
167 |
* <p>
|
|
168 |
* Using this method will prevent the ability to use an alternate clock for testing
|
|
169 |
* because the clock is hard-coded.
|
|
170 |
*
|
|
171 |
* @return the current date using the system clock and default time-zone, not null
|
|
172 |
*/
|
|
173 |
public static HijrahDate now() {
|
|
174 |
return now(Clock.systemDefaultZone());
|
|
175 |
}
|
|
176 |
|
|
177 |
/**
|
16852
|
178 |
* Obtains the current {@code HijrahDate} of the Islamic Umm Al-Qura calendar
|
|
179 |
* in the specified time-zone.
|
15658
|
180 |
* <p>
|
|
181 |
* This will query the {@link Clock#system(ZoneId) system clock} to obtain the current date.
|
|
182 |
* Specifying the time-zone avoids dependence on the default time-zone.
|
|
183 |
* <p>
|
|
184 |
* Using this method will prevent the ability to use an alternate clock for testing
|
|
185 |
* because the clock is hard-coded.
|
|
186 |
*
|
|
187 |
* @param zone the zone ID to use, not null
|
|
188 |
* @return the current date using the system clock, not null
|
|
189 |
*/
|
|
190 |
public static HijrahDate now(ZoneId zone) {
|
|
191 |
return now(Clock.system(zone));
|
|
192 |
}
|
|
193 |
|
|
194 |
/**
|
16852
|
195 |
* Obtains the current {@code HijrahDate} of the Islamic Umm Al-Qura calendar
|
|
196 |
* from the specified clock.
|
15658
|
197 |
* <p>
|
|
198 |
* This will query the specified clock to obtain the current date - today.
|
|
199 |
* Using this method allows the use of an alternate clock for testing.
|
|
200 |
* The alternate clock may be introduced using {@linkplain Clock dependency injection}.
|
|
201 |
*
|
|
202 |
* @param clock the clock to use, not null
|
|
203 |
* @return the current date, not null
|
|
204 |
* @throws DateTimeException if the current date cannot be obtained
|
|
205 |
*/
|
|
206 |
public static HijrahDate now(Clock clock) {
|
19030
|
207 |
return HijrahDate.ofEpochDay(HijrahChronology.INSTANCE, LocalDate.now(clock).toEpochDay());
|
15658
|
208 |
}
|
|
209 |
|
|
210 |
/**
|
16852
|
211 |
* Obtains a {@code HijrahDate} of the Islamic Umm Al-Qura calendar
|
|
212 |
* from the proleptic-year, month-of-year and day-of-month fields.
|
15658
|
213 |
* <p>
|
|
214 |
* This returns a {@code HijrahDate} with the specified fields.
|
|
215 |
* The day must be valid for the year and month, otherwise an exception will be thrown.
|
|
216 |
*
|
|
217 |
* @param prolepticYear the Hijrah proleptic-year
|
|
218 |
* @param month the Hijrah month-of-year, from 1 to 12
|
|
219 |
* @param dayOfMonth the Hijrah day-of-month, from 1 to 30
|
|
220 |
* @return the date in Hijrah calendar system, not null
|
|
221 |
* @throws DateTimeException if the value of any field is out of range,
|
|
222 |
* or if the day-of-month is invalid for the month-year
|
|
223 |
*/
|
|
224 |
public static HijrahDate of(int prolepticYear, int month, int dayOfMonth) {
|
|
225 |
return HijrahChronology.INSTANCE.date(prolepticYear, month, dayOfMonth);
|
|
226 |
}
|
|
227 |
|
|
228 |
/**
|
16852
|
229 |
* Obtains a {@code HijrahDate} of the Islamic Umm Al-Qura calendar from a temporal object.
|
15658
|
230 |
* <p>
|
|
231 |
* This obtains a date in the Hijrah calendar system based on the specified temporal.
|
|
232 |
* A {@code TemporalAccessor} represents an arbitrary set of date and time information,
|
|
233 |
* which this factory converts to an instance of {@code HijrahDate}.
|
|
234 |
* <p>
|
|
235 |
* The conversion typically uses the {@link ChronoField#EPOCH_DAY EPOCH_DAY}
|
|
236 |
* field, which is standardized across calendar systems.
|
|
237 |
* <p>
|
|
238 |
* This method matches the signature of the functional interface {@link TemporalQuery}
|
|
239 |
* allowing it to be used as a query via method reference, {@code HijrahDate::from}.
|
|
240 |
*
|
|
241 |
* @param temporal the temporal object to convert, not null
|
|
242 |
* @return the date in Hijrah calendar system, not null
|
|
243 |
* @throws DateTimeException if unable to convert to a {@code HijrahDate}
|
|
244 |
*/
|
|
245 |
public static HijrahDate from(TemporalAccessor temporal) {
|
|
246 |
return HijrahChronology.INSTANCE.date(temporal);
|
|
247 |
}
|
|
248 |
|
|
249 |
//-----------------------------------------------------------------------
|
|
250 |
/**
|
16852
|
251 |
* Constructs an {@code HijrahDate} with the proleptic-year, month-of-year and
|
|
252 |
* day-of-month fields.
|
15658
|
253 |
*
|
16852
|
254 |
* @param chrono The chronology to create the date with
|
|
255 |
* @param prolepticYear the proleptic year
|
|
256 |
* @param monthOfYear the month of year
|
|
257 |
* @param dayOfMonth the day of month
|
15658
|
258 |
*/
|
16852
|
259 |
private HijrahDate(HijrahChronology chrono, int prolepticYear, int monthOfYear, int dayOfMonth) {
|
|
260 |
// Computing the Gregorian day checks the valid ranges
|
|
261 |
chrono.getEpochDay(prolepticYear, monthOfYear, dayOfMonth);
|
15658
|
262 |
|
16852
|
263 |
this.chrono = chrono;
|
|
264 |
this.prolepticYear = prolepticYear;
|
|
265 |
this.monthOfYear = monthOfYear;
|
|
266 |
this.dayOfMonth = dayOfMonth;
|
|
267 |
}
|
15658
|
268 |
|
16852
|
269 |
/**
|
|
270 |
* Constructs an instance with the Epoch Day.
|
|
271 |
*
|
|
272 |
* @param epochDay the epochDay
|
|
273 |
*/
|
|
274 |
private HijrahDate(HijrahChronology chrono, long epochDay) {
|
|
275 |
int[] dateInfo = chrono.getHijrahDateInfo((int)epochDay);
|
|
276 |
|
|
277 |
this.chrono = chrono;
|
|
278 |
this.prolepticYear = dateInfo[0];
|
|
279 |
this.monthOfYear = dateInfo[1];
|
|
280 |
this.dayOfMonth = dateInfo[2];
|
15658
|
281 |
}
|
|
282 |
|
|
283 |
//-----------------------------------------------------------------------
|
16852
|
284 |
/**
|
|
285 |
* Gets the chronology of this date, which is the Hijrah calendar system.
|
|
286 |
* <p>
|
|
287 |
* The {@code Chronology} represents the calendar system in use.
|
|
288 |
* The era and other fields in {@link ChronoField} are defined by the chronology.
|
|
289 |
*
|
|
290 |
* @return the Hijrah chronology, not null
|
|
291 |
*/
|
15658
|
292 |
@Override
|
|
293 |
public HijrahChronology getChronology() {
|
|
294 |
return chrono;
|
|
295 |
}
|
|
296 |
|
16852
|
297 |
/**
|
|
298 |
* Gets the era applicable at this date.
|
|
299 |
* <p>
|
|
300 |
* The Hijrah calendar system has one era, 'AH',
|
|
301 |
* defined by {@link HijrahEra}.
|
|
302 |
*
|
|
303 |
* @return the era applicable at this date, not null
|
|
304 |
*/
|
|
305 |
@Override
|
|
306 |
public HijrahEra getEra() {
|
|
307 |
return HijrahEra.AH;
|
|
308 |
}
|
|
309 |
|
|
310 |
/**
|
|
311 |
* Returns the length of the month represented by this date.
|
|
312 |
* <p>
|
|
313 |
* This returns the length of the month in days.
|
|
314 |
* Month lengths in the Hijrah calendar system vary between 29 and 30 days.
|
|
315 |
*
|
|
316 |
* @return the length of the month in days
|
|
317 |
*/
|
|
318 |
@Override
|
|
319 |
public int lengthOfMonth() {
|
|
320 |
return chrono.getMonthLength(prolepticYear, monthOfYear);
|
|
321 |
}
|
|
322 |
|
|
323 |
/**
|
|
324 |
* Returns the length of the year represented by this date.
|
|
325 |
* <p>
|
|
326 |
* This returns the length of the year in days.
|
|
327 |
* A Hijrah calendar system year is typically shorter than
|
|
328 |
* that of the ISO calendar system.
|
|
329 |
*
|
|
330 |
* @return the length of the year in days
|
|
331 |
*/
|
|
332 |
@Override
|
|
333 |
public int lengthOfYear() {
|
|
334 |
return chrono.getYearLength(prolepticYear);
|
|
335 |
}
|
|
336 |
|
|
337 |
//-----------------------------------------------------------------------
|
15658
|
338 |
@Override
|
|
339 |
public ValueRange range(TemporalField field) {
|
|
340 |
if (field instanceof ChronoField) {
|
|
341 |
if (isSupported(field)) {
|
|
342 |
ChronoField f = (ChronoField) field;
|
|
343 |
switch (f) {
|
|
344 |
case DAY_OF_MONTH: return ValueRange.of(1, lengthOfMonth());
|
|
345 |
case DAY_OF_YEAR: return ValueRange.of(1, lengthOfYear());
|
|
346 |
case ALIGNED_WEEK_OF_MONTH: return ValueRange.of(1, 5); // TODO
|
16852
|
347 |
// TODO does the limited range of valid years cause years to
|
|
348 |
// start/end part way through? that would affect range
|
15658
|
349 |
}
|
|
350 |
return getChronology().range(f);
|
|
351 |
}
|
19030
|
352 |
throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
|
15658
|
353 |
}
|
|
354 |
return field.rangeRefinedBy(this);
|
|
355 |
}
|
|
356 |
|
|
357 |
@Override
|
|
358 |
public long getLong(TemporalField field) {
|
|
359 |
if (field instanceof ChronoField) {
|
|
360 |
switch ((ChronoField) field) {
|
16852
|
361 |
case DAY_OF_WEEK: return getDayOfWeek();
|
|
362 |
case ALIGNED_DAY_OF_WEEK_IN_MONTH: return ((getDayOfWeek() - 1) % 7) + 1;
|
|
363 |
case ALIGNED_DAY_OF_WEEK_IN_YEAR: return ((getDayOfYear() - 1) % 7) + 1;
|
15658
|
364 |
case DAY_OF_MONTH: return this.dayOfMonth;
|
16852
|
365 |
case DAY_OF_YEAR: return this.getDayOfYear();
|
15658
|
366 |
case EPOCH_DAY: return toEpochDay();
|
|
367 |
case ALIGNED_WEEK_OF_MONTH: return ((dayOfMonth - 1) / 7) + 1;
|
16852
|
368 |
case ALIGNED_WEEK_OF_YEAR: return ((getDayOfYear() - 1) / 7) + 1;
|
15658
|
369 |
case MONTH_OF_YEAR: return monthOfYear;
|
16852
|
370 |
case PROLEPTIC_MONTH: return getProlepticMonth();
|
|
371 |
case YEAR_OF_ERA: return prolepticYear;
|
|
372 |
case YEAR: return prolepticYear;
|
|
373 |
case ERA: return getEraValue();
|
15658
|
374 |
}
|
19030
|
375 |
throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
|
15658
|
376 |
}
|
|
377 |
return field.getFrom(this);
|
|
378 |
}
|
|
379 |
|
16852
|
380 |
private long getProlepticMonth() {
|
|
381 |
return prolepticYear * 12L + monthOfYear - 1;
|
|
382 |
}
|
|
383 |
|
15658
|
384 |
@Override
|
|
385 |
public HijrahDate with(TemporalField field, long newValue) {
|
|
386 |
if (field instanceof ChronoField) {
|
|
387 |
ChronoField f = (ChronoField) field;
|
16852
|
388 |
// not using checkValidIntValue so EPOCH_DAY and PROLEPTIC_MONTH work
|
|
389 |
chrono.range(f).checkValidValue(newValue, f); // TODO: validate value
|
15658
|
390 |
int nvalue = (int) newValue;
|
|
391 |
switch (f) {
|
16852
|
392 |
case DAY_OF_WEEK: return plusDays(newValue - getDayOfWeek());
|
15658
|
393 |
case ALIGNED_DAY_OF_WEEK_IN_MONTH: return plusDays(newValue - getLong(ALIGNED_DAY_OF_WEEK_IN_MONTH));
|
|
394 |
case ALIGNED_DAY_OF_WEEK_IN_YEAR: return plusDays(newValue - getLong(ALIGNED_DAY_OF_WEEK_IN_YEAR));
|
16852
|
395 |
case DAY_OF_MONTH: return resolvePreviousValid(prolepticYear, monthOfYear, nvalue);
|
19030
|
396 |
case DAY_OF_YEAR: return plusDays(Math.min(nvalue, lengthOfYear()) - getDayOfYear());
|
16852
|
397 |
case EPOCH_DAY: return new HijrahDate(chrono, newValue);
|
15658
|
398 |
case ALIGNED_WEEK_OF_MONTH: return plusDays((newValue - getLong(ALIGNED_WEEK_OF_MONTH)) * 7);
|
|
399 |
case ALIGNED_WEEK_OF_YEAR: return plusDays((newValue - getLong(ALIGNED_WEEK_OF_YEAR)) * 7);
|
16852
|
400 |
case MONTH_OF_YEAR: return resolvePreviousValid(prolepticYear, nvalue, dayOfMonth);
|
|
401 |
case PROLEPTIC_MONTH: return plusMonths(newValue - getProlepticMonth());
|
|
402 |
case YEAR_OF_ERA: return resolvePreviousValid(prolepticYear >= 1 ? nvalue : 1 - nvalue, monthOfYear, dayOfMonth);
|
15658
|
403 |
case YEAR: return resolvePreviousValid(nvalue, monthOfYear, dayOfMonth);
|
16852
|
404 |
case ERA: return resolvePreviousValid(1 - prolepticYear, monthOfYear, dayOfMonth);
|
15658
|
405 |
}
|
19030
|
406 |
throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
|
15658
|
407 |
}
|
19030
|
408 |
return super.with(field, newValue);
|
15658
|
409 |
}
|
|
410 |
|
16852
|
411 |
private HijrahDate resolvePreviousValid(int prolepticYear, int month, int day) {
|
|
412 |
int monthDays = chrono.getMonthLength(prolepticYear, month);
|
15658
|
413 |
if (day > monthDays) {
|
|
414 |
day = monthDays;
|
|
415 |
}
|
16852
|
416 |
return HijrahDate.of(chrono, prolepticYear, month, day);
|
15658
|
417 |
}
|
|
418 |
|
|
419 |
/**
|
|
420 |
* {@inheritDoc}
|
16852
|
421 |
* @throws DateTimeException if unable to make the adjustment.
|
|
422 |
* For example, if the adjuster requires an ISO chronology
|
15658
|
423 |
* @throws ArithmeticException {@inheritDoc}
|
|
424 |
*/
|
|
425 |
@Override
|
|
426 |
public HijrahDate with(TemporalAdjuster adjuster) {
|
16852
|
427 |
return super.with(adjuster);
|
|
428 |
}
|
|
429 |
|
|
430 |
/**
|
|
431 |
* Returns a {@code HijrahDate} with the Chronology requested.
|
|
432 |
* <p>
|
|
433 |
* The year, month, and day are checked against the new requested
|
|
434 |
* HijrahChronology. If the chronology has a shorter month length
|
|
435 |
* for the month, the day is reduced to be the last day of the month.
|
|
436 |
*
|
|
437 |
* @param chronology the new HijrahChonology, non-null
|
|
438 |
* @return a HijrahDate with the requested HijrahChronology, non-null
|
|
439 |
*/
|
|
440 |
public HijrahDate withVariant(HijrahChronology chronology) {
|
|
441 |
if (chrono == chronology) {
|
|
442 |
return this;
|
|
443 |
}
|
|
444 |
// Like resolvePreviousValid the day is constrained to stay in the same month
|
|
445 |
int monthDays = chronology.getDayOfYear(prolepticYear, monthOfYear);
|
|
446 |
return HijrahDate.of(chronology, prolepticYear, monthOfYear,(dayOfMonth > monthDays) ? monthDays : dayOfMonth );
|
15658
|
447 |
}
|
|
448 |
|
|
449 |
/**
|
|
450 |
* {@inheritDoc}
|
|
451 |
* @throws DateTimeException {@inheritDoc}
|
|
452 |
* @throws ArithmeticException {@inheritDoc}
|
|
453 |
*/
|
|
454 |
@Override
|
|
455 |
public HijrahDate plus(TemporalAmount amount) {
|
16852
|
456 |
return super.plus(amount);
|
15658
|
457 |
}
|
|
458 |
|
|
459 |
/**
|
|
460 |
* {@inheritDoc}
|
|
461 |
* @throws DateTimeException {@inheritDoc}
|
|
462 |
* @throws ArithmeticException {@inheritDoc}
|
|
463 |
*/
|
|
464 |
@Override
|
|
465 |
public HijrahDate minus(TemporalAmount amount) {
|
16852
|
466 |
return super.minus(amount);
|
15658
|
467 |
}
|
|
468 |
|
|
469 |
@Override
|
|
470 |
public long toEpochDay() {
|
16852
|
471 |
return chrono.getEpochDay(prolepticYear, monthOfYear, dayOfMonth);
|
|
472 |
}
|
|
473 |
|
|
474 |
/**
|
|
475 |
* Gets the day-of-year field.
|
|
476 |
* <p>
|
|
477 |
* This method returns the primitive {@code int} value for the day-of-year.
|
|
478 |
*
|
|
479 |
* @return the day-of-year
|
|
480 |
*/
|
|
481 |
private int getDayOfYear() {
|
19030
|
482 |
return chrono.getDayOfYear(prolepticYear, monthOfYear) + dayOfMonth;
|
16852
|
483 |
}
|
|
484 |
|
|
485 |
/**
|
|
486 |
* Gets the day-of-week value.
|
|
487 |
*
|
|
488 |
* @return the day-of-week; computed from the epochday
|
|
489 |
*/
|
|
490 |
private int getDayOfWeek() {
|
|
491 |
int dow0 = (int)Math.floorMod(toEpochDay() + 3, 7);
|
|
492 |
return dow0 + 1;
|
|
493 |
}
|
|
494 |
|
|
495 |
/**
|
|
496 |
* Gets the Era of this date.
|
|
497 |
*
|
|
498 |
* @return the Era of this date; computed from epochDay
|
|
499 |
*/
|
|
500 |
private int getEraValue() {
|
|
501 |
return (prolepticYear > 1 ? 1 : 0);
|
15658
|
502 |
}
|
|
503 |
|
|
504 |
//-----------------------------------------------------------------------
|
|
505 |
/**
|
|
506 |
* Checks if the year is a leap year, according to the Hijrah calendar system rules.
|
|
507 |
*
|
|
508 |
* @return true if this date is in a leap year
|
|
509 |
*/
|
|
510 |
@Override
|
|
511 |
public boolean isLeapYear() {
|
16852
|
512 |
return chrono.isLeapYear(prolepticYear);
|
15658
|
513 |
}
|
|
514 |
|
|
515 |
//-----------------------------------------------------------------------
|
|
516 |
@Override
|
|
517 |
HijrahDate plusYears(long years) {
|
|
518 |
if (years == 0) {
|
|
519 |
return this;
|
|
520 |
}
|
16852
|
521 |
int newYear = Math.addExact(this.prolepticYear, (int)years);
|
|
522 |
return resolvePreviousValid(newYear, monthOfYear, dayOfMonth);
|
15658
|
523 |
}
|
|
524 |
|
|
525 |
@Override
|
16852
|
526 |
HijrahDate plusMonths(long monthsToAdd) {
|
|
527 |
if (monthsToAdd == 0) {
|
15658
|
528 |
return this;
|
|
529 |
}
|
16852
|
530 |
long monthCount = prolepticYear * 12L + (monthOfYear - 1);
|
|
531 |
long calcMonths = monthCount + monthsToAdd; // safe overflow
|
|
532 |
int newYear = chrono.checkValidYear(Math.floorDiv(calcMonths, 12L));
|
|
533 |
int newMonth = (int)Math.floorMod(calcMonths, 12L) + 1;
|
|
534 |
return resolvePreviousValid(newYear, newMonth, dayOfMonth);
|
15658
|
535 |
}
|
|
536 |
|
|
537 |
@Override
|
|
538 |
HijrahDate plusWeeks(long weeksToAdd) {
|
16852
|
539 |
return super.plusWeeks(weeksToAdd);
|
15658
|
540 |
}
|
|
541 |
|
|
542 |
@Override
|
|
543 |
HijrahDate plusDays(long days) {
|
16852
|
544 |
return new HijrahDate(chrono, toEpochDay() + days);
|
15658
|
545 |
}
|
|
546 |
|
|
547 |
@Override
|
|
548 |
public HijrahDate plus(long amountToAdd, TemporalUnit unit) {
|
16852
|
549 |
return super.plus(amountToAdd, unit);
|
15658
|
550 |
}
|
|
551 |
|
|
552 |
@Override
|
|
553 |
public HijrahDate minus(long amountToSubtract, TemporalUnit unit) {
|
16852
|
554 |
return super.minus(amountToSubtract, unit);
|
15658
|
555 |
}
|
|
556 |
|
|
557 |
@Override
|
|
558 |
HijrahDate minusYears(long yearsToSubtract) {
|
16852
|
559 |
return super.minusYears(yearsToSubtract);
|
15658
|
560 |
}
|
|
561 |
|
|
562 |
@Override
|
|
563 |
HijrahDate minusMonths(long monthsToSubtract) {
|
16852
|
564 |
return super.minusMonths(monthsToSubtract);
|
15658
|
565 |
}
|
|
566 |
|
|
567 |
@Override
|
|
568 |
HijrahDate minusWeeks(long weeksToSubtract) {
|
16852
|
569 |
return super.minusWeeks(weeksToSubtract);
|
15658
|
570 |
}
|
|
571 |
|
|
572 |
@Override
|
|
573 |
HijrahDate minusDays(long daysToSubtract) {
|
16852
|
574 |
return super.minusDays(daysToSubtract);
|
15658
|
575 |
}
|
|
576 |
|
|
577 |
@Override // for javadoc and covariant return type
|
19030
|
578 |
@SuppressWarnings("unchecked")
|
15658
|
579 |
public final ChronoLocalDateTime<HijrahDate> atTime(LocalTime localTime) {
|
19030
|
580 |
return (ChronoLocalDateTime<HijrahDate>)super.atTime(localTime);
|
15658
|
581 |
}
|
|
582 |
|
|
583 |
@Override
|
19030
|
584 |
public Period until(ChronoLocalDate endDate) {
|
15658
|
585 |
// TODO: untested
|
16852
|
586 |
HijrahDate end = getChronology().date(endDate);
|
|
587 |
long totalMonths = (end.prolepticYear - this.prolepticYear) * 12 + (end.monthOfYear - this.monthOfYear); // safe
|
15658
|
588 |
int days = end.dayOfMonth - this.dayOfMonth;
|
|
589 |
if (totalMonths > 0 && days < 0) {
|
|
590 |
totalMonths--;
|
|
591 |
HijrahDate calcDate = this.plusMonths(totalMonths);
|
|
592 |
days = (int) (end.toEpochDay() - calcDate.toEpochDay()); // safe
|
|
593 |
} else if (totalMonths < 0 && days > 0) {
|
|
594 |
totalMonths++;
|
|
595 |
days -= end.lengthOfMonth();
|
|
596 |
}
|
|
597 |
long years = totalMonths / 12; // safe
|
|
598 |
int months = (int) (totalMonths % 12); // safe
|
|
599 |
return Period.of(Math.toIntExact(years), months, days);
|
|
600 |
}
|
|
601 |
|
|
602 |
//-----------------------------------------------------------------------
|
|
603 |
private Object writeReplace() {
|
|
604 |
return new Ser(Ser.HIJRAH_DATE_TYPE, this);
|
|
605 |
}
|
|
606 |
|
|
607 |
void writeExternal(ObjectOutput out) throws IOException {
|
|
608 |
// HijrahChronology is implicit in the Hijrah_DATE_TYPE
|
|
609 |
out.writeObject(chrono);
|
|
610 |
out.writeInt(get(YEAR));
|
|
611 |
out.writeByte(get(MONTH_OF_YEAR));
|
|
612 |
out.writeByte(get(DAY_OF_MONTH));
|
|
613 |
}
|
|
614 |
|
|
615 |
/**
|
|
616 |
* Replaces the date instance from the stream with a valid one.
|
|
617 |
* ReadExternal has already read the fields and created a new instance
|
|
618 |
* from the data.
|
|
619 |
*
|
|
620 |
* @return the resolved date, never null
|
|
621 |
*/
|
|
622 |
private Object readResolve() {
|
|
623 |
return this;
|
|
624 |
}
|
|
625 |
|
19030
|
626 |
static HijrahDate readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
|
16852
|
627 |
HijrahChronology chrono = (HijrahChronology) in.readObject();
|
15658
|
628 |
int year = in.readInt();
|
|
629 |
int month = in.readByte();
|
|
630 |
int dayOfMonth = in.readByte();
|
|
631 |
return chrono.date(year, month, dayOfMonth);
|
|
632 |
}
|
|
633 |
|
|
634 |
}
|