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.DAY_OF_MONTH;
|
|
60 |
import static java.time.temporal.ChronoField.ERA;
|
|
61 |
import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
|
16852
|
62 |
import static java.time.temporal.ChronoField.PROLEPTIC_MONTH;
|
15658
|
63 |
import static java.time.temporal.ChronoField.YEAR_OF_ERA;
|
|
64 |
|
|
65 |
import java.io.Serializable;
|
|
66 |
import java.time.DateTimeException;
|
|
67 |
import java.time.temporal.ChronoUnit;
|
|
68 |
import java.time.temporal.Temporal;
|
|
69 |
import java.time.temporal.TemporalAdjuster;
|
|
70 |
import java.time.temporal.TemporalUnit;
|
16852
|
71 |
import java.time.temporal.UnsupportedTemporalTypeException;
|
|
72 |
import java.time.temporal.ValueRange;
|
|
73 |
import java.util.Objects;
|
15658
|
74 |
|
|
75 |
/**
|
|
76 |
* A date expressed in terms of a standard year-month-day calendar system.
|
|
77 |
* <p>
|
|
78 |
* This class is used by applications seeking to handle dates in non-ISO calendar systems.
|
|
79 |
* For example, the Japanese, Minguo, Thai Buddhist and others.
|
|
80 |
* <p>
|
|
81 |
* {@code ChronoLocalDate} is built on the generic concepts of year, month and day.
|
|
82 |
* The calendar system, represented by a {@link java.time.chrono.Chronology}, expresses the relationship between
|
|
83 |
* the fields and this class allows the resulting date to be manipulated.
|
|
84 |
* <p>
|
|
85 |
* Note that not all calendar systems are suitable for use with this class.
|
|
86 |
* For example, the Mayan calendar uses a system that bears no relation to years, months and days.
|
|
87 |
* <p>
|
|
88 |
* The API design encourages the use of {@code LocalDate} for the majority of the application.
|
|
89 |
* This includes code to read and write from a persistent data store, such as a database,
|
|
90 |
* and to send dates and times across a network. The {@code ChronoLocalDate} instance is then used
|
|
91 |
* at the user interface level to deal with localized input/output.
|
|
92 |
*
|
|
93 |
* <P>Example: </p>
|
|
94 |
* <pre>
|
|
95 |
* System.out.printf("Example()%n");
|
|
96 |
* // Enumerate the list of available calendars and print today for each
|
|
97 |
* Set<Chronology> chronos = Chronology.getAvailableChronologies();
|
|
98 |
* for (Chronology chrono : chronos) {
|
16852
|
99 |
* ChronoLocalDate<?> date = chrono.dateNow();
|
15658
|
100 |
* System.out.printf(" %20s: %s%n", chrono.getID(), date.toString());
|
|
101 |
* }
|
|
102 |
*
|
|
103 |
* // Print the Hijrah date and calendar
|
16852
|
104 |
* ChronoLocalDate<?> date = Chronology.of("Hijrah").dateNow();
|
15658
|
105 |
* int day = date.get(ChronoField.DAY_OF_MONTH);
|
|
106 |
* int dow = date.get(ChronoField.DAY_OF_WEEK);
|
|
107 |
* int month = date.get(ChronoField.MONTH_OF_YEAR);
|
|
108 |
* int year = date.get(ChronoField.YEAR);
|
|
109 |
* System.out.printf(" Today is %s %s %d-%s-%d%n", date.getChronology().getID(),
|
|
110 |
* dow, day, month, year);
|
|
111 |
|
|
112 |
* // Print today's date and the last day of the year
|
16852
|
113 |
* ChronoLocalDate<?> now1 = Chronology.of("Hijrah").dateNow();
|
|
114 |
* ChronoLocalDate<?> first = now1.with(ChronoField.DAY_OF_MONTH, 1)
|
15658
|
115 |
* .with(ChronoField.MONTH_OF_YEAR, 1);
|
16852
|
116 |
* ChronoLocalDate<?> last = first.plus(1, ChronoUnit.YEARS)
|
15658
|
117 |
* .minus(1, ChronoUnit.DAYS);
|
|
118 |
* System.out.printf(" Today is %s: start: %s; end: %s%n", last.getChronology().getID(),
|
|
119 |
* first, last);
|
|
120 |
* </pre>
|
|
121 |
*
|
|
122 |
* <h3>Adding Calendars</h3>
|
|
123 |
* <p> The set of calendars is extensible by defining a subclass of {@link ChronoLocalDate}
|
|
124 |
* to represent a date instance and an implementation of {@code Chronology}
|
|
125 |
* to be the factory for the ChronoLocalDate subclass.
|
|
126 |
* </p>
|
|
127 |
* <p> To permit the discovery of the additional calendar types the implementation of
|
|
128 |
* {@code Chronology} must be registered as a Service implementing the {@code Chronology} interface
|
|
129 |
* in the {@code META-INF/Services} file as per the specification of {@link java.util.ServiceLoader}.
|
|
130 |
* The subclass must function according to the {@code Chronology} class description and must provide its
|
|
131 |
* {@link java.time.chrono.Chronology#getId() chronlogy ID} and {@link Chronology#getCalendarType() calendar type}. </p>
|
|
132 |
*
|
17474
|
133 |
* @implSpec
|
15658
|
134 |
* This abstract class must be implemented with care to ensure other classes operate correctly.
|
|
135 |
* All implementations that can be instantiated must be final, immutable and thread-safe.
|
|
136 |
* Subclasses should be Serializable wherever possible.
|
|
137 |
*
|
|
138 |
* @param <D> the ChronoLocalDate of this date-time
|
|
139 |
* @since 1.8
|
|
140 |
*/
|
|
141 |
abstract class ChronoDateImpl<D extends ChronoLocalDate<D>>
|
|
142 |
implements ChronoLocalDate<D>, Temporal, TemporalAdjuster, Serializable {
|
|
143 |
|
|
144 |
/**
|
|
145 |
* Serialization version.
|
|
146 |
*/
|
|
147 |
private static final long serialVersionUID = 6282433883239719096L;
|
|
148 |
|
|
149 |
/**
|
|
150 |
* Creates an instance.
|
|
151 |
*/
|
|
152 |
ChronoDateImpl() {
|
|
153 |
}
|
|
154 |
|
|
155 |
//-----------------------------------------------------------------------
|
|
156 |
@Override
|
|
157 |
public D plus(long amountToAdd, TemporalUnit unit) {
|
|
158 |
if (unit instanceof ChronoUnit) {
|
|
159 |
ChronoUnit f = (ChronoUnit) unit;
|
|
160 |
switch (f) {
|
|
161 |
case DAYS: return plusDays(amountToAdd);
|
|
162 |
case WEEKS: return plusDays(Math.multiplyExact(amountToAdd, 7));
|
|
163 |
case MONTHS: return plusMonths(amountToAdd);
|
|
164 |
case YEARS: return plusYears(amountToAdd);
|
|
165 |
case DECADES: return plusYears(Math.multiplyExact(amountToAdd, 10));
|
|
166 |
case CENTURIES: return plusYears(Math.multiplyExact(amountToAdd, 100));
|
|
167 |
case MILLENNIA: return plusYears(Math.multiplyExact(amountToAdd, 1000));
|
|
168 |
case ERAS: return with(ERA, Math.addExact(getLong(ERA), amountToAdd));
|
|
169 |
}
|
16852
|
170 |
throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit.getName());
|
15658
|
171 |
}
|
|
172 |
return ChronoLocalDate.super.plus(amountToAdd, unit);
|
|
173 |
}
|
|
174 |
|
|
175 |
//-----------------------------------------------------------------------
|
|
176 |
/**
|
|
177 |
* Returns a copy of this date with the specified period in years added.
|
|
178 |
* <p>
|
|
179 |
* This adds the specified period in years to the date.
|
|
180 |
* In some cases, adding years can cause the resulting date to become invalid.
|
|
181 |
* If this occurs, then other fields, typically the day-of-month, will be adjusted to ensure
|
|
182 |
* that the result is valid. Typically this will select the last valid day of the month.
|
|
183 |
* <p>
|
|
184 |
* This instance is immutable and unaffected by this method call.
|
|
185 |
*
|
|
186 |
* @param yearsToAdd the years to add, may be negative
|
|
187 |
* @return a date based on this one with the years added, not null
|
|
188 |
* @throws DateTimeException if the result exceeds the supported date range
|
|
189 |
*/
|
|
190 |
abstract D plusYears(long yearsToAdd);
|
|
191 |
|
|
192 |
/**
|
|
193 |
* Returns a copy of this date with the specified period in months added.
|
|
194 |
* <p>
|
|
195 |
* This adds the specified period in months to the date.
|
|
196 |
* In some cases, adding months can cause the resulting date to become invalid.
|
|
197 |
* If this occurs, then other fields, typically the day-of-month, will be adjusted to ensure
|
|
198 |
* that the result is valid. Typically this will select the last valid day of the month.
|
|
199 |
* <p>
|
|
200 |
* This instance is immutable and unaffected by this method call.
|
|
201 |
*
|
|
202 |
* @param monthsToAdd the months to add, may be negative
|
|
203 |
* @return a date based on this one with the months added, not null
|
|
204 |
* @throws DateTimeException if the result exceeds the supported date range
|
|
205 |
*/
|
|
206 |
abstract D plusMonths(long monthsToAdd);
|
|
207 |
|
|
208 |
/**
|
|
209 |
* Returns a copy of this date with the specified period in weeks added.
|
|
210 |
* <p>
|
|
211 |
* This adds the specified period in weeks to the date.
|
|
212 |
* In some cases, adding weeks can cause the resulting date to become invalid.
|
|
213 |
* If this occurs, then other fields will be adjusted to ensure that the result is valid.
|
|
214 |
* <p>
|
|
215 |
* The default implementation uses {@link #plusDays(long)} using a 7 day week.
|
|
216 |
* <p>
|
|
217 |
* This instance is immutable and unaffected by this method call.
|
|
218 |
*
|
|
219 |
* @param weeksToAdd the weeks to add, may be negative
|
|
220 |
* @return a date based on this one with the weeks added, not null
|
|
221 |
* @throws DateTimeException if the result exceeds the supported date range
|
|
222 |
*/
|
|
223 |
D plusWeeks(long weeksToAdd) {
|
|
224 |
return plusDays(Math.multiplyExact(weeksToAdd, 7));
|
|
225 |
}
|
|
226 |
|
|
227 |
/**
|
|
228 |
* Returns a copy of this date with the specified number of days added.
|
|
229 |
* <p>
|
|
230 |
* This adds the specified period in days to the date.
|
|
231 |
* <p>
|
|
232 |
* This instance is immutable and unaffected by this method call.
|
|
233 |
*
|
|
234 |
* @param daysToAdd the days to add, may be negative
|
|
235 |
* @return a date based on this one with the days added, not null
|
|
236 |
* @throws DateTimeException if the result exceeds the supported date range
|
|
237 |
*/
|
|
238 |
abstract D plusDays(long daysToAdd);
|
|
239 |
|
|
240 |
//-----------------------------------------------------------------------
|
|
241 |
/**
|
|
242 |
* Returns a copy of this date with the specified period in years subtracted.
|
|
243 |
* <p>
|
|
244 |
* This subtracts the specified period in years to the date.
|
|
245 |
* In some cases, subtracting years can cause the resulting date to become invalid.
|
|
246 |
* If this occurs, then other fields, typically the day-of-month, will be adjusted to ensure
|
|
247 |
* that the result is valid. Typically this will select the last valid day of the month.
|
|
248 |
* <p>
|
|
249 |
* The default implementation uses {@link #plusYears(long)}.
|
|
250 |
* <p>
|
|
251 |
* This instance is immutable and unaffected by this method call.
|
|
252 |
*
|
|
253 |
* @param yearsToSubtract the years to subtract, may be negative
|
|
254 |
* @return a date based on this one with the years subtracted, not null
|
|
255 |
* @throws DateTimeException if the result exceeds the supported date range
|
|
256 |
*/
|
|
257 |
D minusYears(long yearsToSubtract) {
|
|
258 |
return (yearsToSubtract == Long.MIN_VALUE ? ((ChronoDateImpl<D>)plusYears(Long.MAX_VALUE)).plusYears(1) : plusYears(-yearsToSubtract));
|
|
259 |
}
|
|
260 |
|
|
261 |
/**
|
|
262 |
* Returns a copy of this date with the specified period in months subtracted.
|
|
263 |
* <p>
|
|
264 |
* This subtracts the specified period in months to the date.
|
|
265 |
* In some cases, subtracting months can cause the resulting date to become invalid.
|
|
266 |
* If this occurs, then other fields, typically the day-of-month, will be adjusted to ensure
|
|
267 |
* that the result is valid. Typically this will select the last valid day of the month.
|
|
268 |
* <p>
|
|
269 |
* The default implementation uses {@link #plusMonths(long)}.
|
|
270 |
* <p>
|
|
271 |
* This instance is immutable and unaffected by this method call.
|
|
272 |
*
|
|
273 |
* @param monthsToSubtract the months to subtract, may be negative
|
|
274 |
* @return a date based on this one with the months subtracted, not null
|
|
275 |
* @throws DateTimeException if the result exceeds the supported date range
|
|
276 |
*/
|
|
277 |
D minusMonths(long monthsToSubtract) {
|
|
278 |
return (monthsToSubtract == Long.MIN_VALUE ? ((ChronoDateImpl<D>)plusMonths(Long.MAX_VALUE)).plusMonths(1) : plusMonths(-monthsToSubtract));
|
|
279 |
}
|
|
280 |
|
|
281 |
/**
|
|
282 |
* Returns a copy of this date with the specified period in weeks subtracted.
|
|
283 |
* <p>
|
|
284 |
* This subtracts the specified period in weeks to the date.
|
|
285 |
* In some cases, subtracting weeks can cause the resulting date to become invalid.
|
|
286 |
* If this occurs, then other fields will be adjusted to ensure that the result is valid.
|
|
287 |
* <p>
|
|
288 |
* The default implementation uses {@link #plusWeeks(long)}.
|
|
289 |
* <p>
|
|
290 |
* This instance is immutable and unaffected by this method call.
|
|
291 |
*
|
|
292 |
* @param weeksToSubtract the weeks to subtract, may be negative
|
|
293 |
* @return a date based on this one with the weeks subtracted, not null
|
|
294 |
* @throws DateTimeException if the result exceeds the supported date range
|
|
295 |
*/
|
|
296 |
D minusWeeks(long weeksToSubtract) {
|
|
297 |
return (weeksToSubtract == Long.MIN_VALUE ? ((ChronoDateImpl<D>)plusWeeks(Long.MAX_VALUE)).plusWeeks(1) : plusWeeks(-weeksToSubtract));
|
|
298 |
}
|
|
299 |
|
|
300 |
/**
|
|
301 |
* Returns a copy of this date with the specified number of days subtracted.
|
|
302 |
* <p>
|
|
303 |
* This subtracts the specified period in days to the date.
|
|
304 |
* <p>
|
|
305 |
* The default implementation uses {@link #plusDays(long)}.
|
|
306 |
* <p>
|
|
307 |
* This instance is immutable and unaffected by this method call.
|
|
308 |
*
|
|
309 |
* @param daysToSubtract the days to subtract, may be negative
|
|
310 |
* @return a date based on this one with the days subtracted, not null
|
|
311 |
* @throws DateTimeException if the result exceeds the supported date range
|
|
312 |
*/
|
|
313 |
D minusDays(long daysToSubtract) {
|
|
314 |
return (daysToSubtract == Long.MIN_VALUE ? ((ChronoDateImpl<D>)plusDays(Long.MAX_VALUE)).plusDays(1) : plusDays(-daysToSubtract));
|
|
315 |
}
|
|
316 |
|
|
317 |
//-----------------------------------------------------------------------
|
|
318 |
/**
|
|
319 |
* {@inheritDoc}
|
|
320 |
* @throws DateTimeException {@inheritDoc}
|
|
321 |
* @throws ArithmeticException {@inheritDoc}
|
|
322 |
*/
|
|
323 |
@Override
|
|
324 |
public long periodUntil(Temporal endDateTime, TemporalUnit unit) {
|
16852
|
325 |
Objects.requireNonNull(endDateTime, "endDateTime");
|
|
326 |
Objects.requireNonNull(unit, "unit");
|
15658
|
327 |
if (endDateTime instanceof ChronoLocalDate == false) {
|
17474
|
328 |
throw new DateTimeException("Unable to calculate amount as objects are of two different types");
|
15658
|
329 |
}
|
|
330 |
ChronoLocalDate<?> end = (ChronoLocalDate<?>) endDateTime;
|
|
331 |
if (getChronology().equals(end.getChronology()) == false) {
|
17474
|
332 |
throw new DateTimeException("Unable to calculate amount as objects have different chronologies");
|
15658
|
333 |
}
|
|
334 |
if (unit instanceof ChronoUnit) {
|
16852
|
335 |
switch ((ChronoUnit) unit) {
|
|
336 |
case DAYS: return daysUntil(end);
|
|
337 |
case WEEKS: return daysUntil(end) / 7;
|
|
338 |
case MONTHS: return monthsUntil(end);
|
|
339 |
case YEARS: return monthsUntil(end) / 12;
|
|
340 |
case DECADES: return monthsUntil(end) / 120;
|
|
341 |
case CENTURIES: return monthsUntil(end) / 1200;
|
|
342 |
case MILLENNIA: return monthsUntil(end) / 12000;
|
|
343 |
case ERAS: return end.getLong(ERA) - getLong(ERA);
|
|
344 |
}
|
|
345 |
throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit.getName());
|
15658
|
346 |
}
|
|
347 |
return unit.between(this, endDateTime);
|
|
348 |
}
|
|
349 |
|
16852
|
350 |
private long daysUntil(ChronoLocalDate<?> end) {
|
|
351 |
return end.toEpochDay() - toEpochDay(); // no overflow
|
|
352 |
}
|
|
353 |
|
|
354 |
private long monthsUntil(ChronoLocalDate<?> end) {
|
|
355 |
ValueRange range = getChronology().range(MONTH_OF_YEAR);
|
|
356 |
if (range.getMaximum() != 12) {
|
|
357 |
throw new IllegalStateException("ChronoDateImpl only supports Chronologies with 12 months per year");
|
|
358 |
}
|
|
359 |
long packed1 = getLong(PROLEPTIC_MONTH) * 32L + get(DAY_OF_MONTH); // no overflow
|
|
360 |
long packed2 = end.getLong(PROLEPTIC_MONTH) * 32L + end.get(DAY_OF_MONTH); // no overflow
|
|
361 |
return (packed2 - packed1) / 32;
|
|
362 |
}
|
|
363 |
|
15658
|
364 |
@Override
|
|
365 |
public boolean equals(Object obj) {
|
|
366 |
if (this == obj) {
|
|
367 |
return true;
|
|
368 |
}
|
|
369 |
if (obj instanceof ChronoLocalDate) {
|
|
370 |
return compareTo((ChronoLocalDate<?>) obj) == 0;
|
|
371 |
}
|
|
372 |
return false;
|
|
373 |
}
|
|
374 |
|
|
375 |
@Override
|
|
376 |
public int hashCode() {
|
|
377 |
long epDay = toEpochDay();
|
|
378 |
return getChronology().hashCode() ^ ((int) (epDay ^ (epDay >>> 32)));
|
|
379 |
}
|
|
380 |
|
|
381 |
@Override
|
|
382 |
public String toString() {
|
|
383 |
// getLong() reduces chances of exceptions in toString()
|
|
384 |
long yoe = getLong(YEAR_OF_ERA);
|
|
385 |
long moy = getLong(MONTH_OF_YEAR);
|
|
386 |
long dom = getLong(DAY_OF_MONTH);
|
|
387 |
StringBuilder buf = new StringBuilder(30);
|
|
388 |
buf.append(getChronology().toString())
|
|
389 |
.append(" ")
|
|
390 |
.append(getEra())
|
|
391 |
.append(" ")
|
|
392 |
.append(yoe)
|
|
393 |
.append(moy < 10 ? "-0" : "-").append(moy)
|
|
394 |
.append(dom < 10 ? "-0" : "-").append(dom);
|
|
395 |
return buf.toString();
|
|
396 |
}
|
|
397 |
|
|
398 |
}
|