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 |
* 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) 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.chrono;
|
|
63 |
|
|
64 |
import java.io.Serializable;
|
|
65 |
import java.time.Clock;
|
|
66 |
import java.time.DateTimeException;
|
|
67 |
import java.time.Instant;
|
|
68 |
import java.time.LocalDate;
|
|
69 |
import java.time.LocalDateTime;
|
|
70 |
import java.time.ZoneId;
|
|
71 |
import java.time.ZonedDateTime;
|
|
72 |
import java.time.temporal.ChronoField;
|
|
73 |
import java.time.temporal.TemporalAccessor;
|
|
74 |
import java.time.temporal.ValueRange;
|
|
75 |
import java.util.Arrays;
|
|
76 |
import java.util.List;
|
|
77 |
import java.util.Locale;
|
|
78 |
import java.util.Objects;
|
|
79 |
|
|
80 |
/**
|
|
81 |
* The ISO calendar system.
|
|
82 |
* <p>
|
|
83 |
* This chronology defines the rules of the ISO calendar system.
|
|
84 |
* This calendar system is based on the ISO-8601 standard, which is the
|
|
85 |
* <i>de facto</i> world calendar.
|
|
86 |
* <p>
|
|
87 |
* The fields are defined as follows:
|
|
88 |
* <p><ul>
|
|
89 |
* <li>era - There are two eras, 'Current Era' (CE) and 'Before Current Era' (BCE).
|
|
90 |
* <li>year-of-era - The year-of-era is the same as the proleptic-year for the current CE era.
|
|
91 |
* For the BCE era before the ISO epoch the year increases from 1 upwards as time goes backwards.
|
|
92 |
* <li>proleptic-year - The proleptic year is the same as the year-of-era for the
|
|
93 |
* current era. For the previous era, years have zero, then negative values.
|
|
94 |
* <li>month-of-year - There are 12 months in an ISO year, numbered from 1 to 12.
|
|
95 |
* <li>day-of-month - There are between 28 and 31 days in each of the ISO month, numbered from 1 to 31.
|
|
96 |
* Months 4, 6, 9 and 11 have 30 days, Months 1, 3, 5, 7, 8, 10 and 12 have 31 days.
|
|
97 |
* Month 2 has 28 days, or 29 in a leap year.
|
|
98 |
* <li>day-of-year - There are 365 days in a standard ISO year and 366 in a leap year.
|
|
99 |
* The days are numbered from 1 to 365 or 1 to 366.
|
|
100 |
* <li>leap-year - Leap years occur every 4 years, except where the year is divisble by 100 and not divisble by 400.
|
|
101 |
* </ul><p>
|
|
102 |
*
|
|
103 |
* <h3>Specification for implementors</h3>
|
|
104 |
* This class is immutable and thread-safe.
|
|
105 |
*
|
|
106 |
* @since 1.8
|
|
107 |
*/
|
|
108 |
public final class IsoChronology extends Chronology implements Serializable {
|
|
109 |
|
|
110 |
/**
|
|
111 |
* Singleton instance of the ISO chronology.
|
|
112 |
*/
|
|
113 |
public static final IsoChronology INSTANCE = new IsoChronology();
|
|
114 |
/**
|
|
115 |
* The singleton instance for the era BCE - 'Before Current Era'.
|
|
116 |
* The 'ISO' part of the name emphasizes that this differs from the BCE
|
|
117 |
* era in the Gregorian calendar system.
|
|
118 |
* This has the numeric value of {@code 0}.
|
|
119 |
*/
|
|
120 |
public static final Era ERA_BCE = IsoEra.BCE;
|
|
121 |
/**
|
|
122 |
* The singleton instance for the era CE - 'Current Era'.
|
|
123 |
* The 'ISO' part of the name emphasizes that this differs from the CE
|
|
124 |
* era in the Gregorian calendar system.
|
|
125 |
* This has the numeric value of {@code 1}.
|
|
126 |
*/
|
|
127 |
public static final Era ERA_CE = IsoEra.CE;
|
|
128 |
|
|
129 |
/**
|
|
130 |
* Serialization version.
|
|
131 |
*/
|
|
132 |
private static final long serialVersionUID = -1440403870442975015L;
|
|
133 |
|
|
134 |
/**
|
|
135 |
* Restricted constructor.
|
|
136 |
*/
|
|
137 |
private IsoChronology() {
|
|
138 |
}
|
|
139 |
|
|
140 |
/**
|
|
141 |
* Resolve singleton.
|
|
142 |
*
|
|
143 |
* @return the singleton instance, not null
|
|
144 |
*/
|
|
145 |
private Object readResolve() {
|
|
146 |
return INSTANCE;
|
|
147 |
}
|
|
148 |
|
|
149 |
//-----------------------------------------------------------------------
|
|
150 |
/**
|
|
151 |
* Gets the ID of the chronology - 'ISO'.
|
|
152 |
* <p>
|
|
153 |
* The ID uniquely identifies the {@code Chronology}.
|
|
154 |
* It can be used to lookup the {@code Chronology} using {@link #of(String)}.
|
|
155 |
*
|
|
156 |
* @return the chronology ID - 'ISO'
|
|
157 |
* @see #getCalendarType()
|
|
158 |
*/
|
|
159 |
@Override
|
|
160 |
public String getId() {
|
|
161 |
return "ISO";
|
|
162 |
}
|
|
163 |
|
|
164 |
/**
|
|
165 |
* Gets the calendar type of the underlying calendar system - 'iso8601'.
|
|
166 |
* <p>
|
|
167 |
* The calendar type is an identifier defined by the
|
|
168 |
* <em>Unicode Locale Data Markup Language (LDML)</em> specification.
|
|
169 |
* It can be used to lookup the {@code Chronology} using {@link #of(String)}.
|
|
170 |
* It can also be used as part of a locale, accessible via
|
|
171 |
* {@link Locale#getUnicodeLocaleType(String)} with the key 'ca'.
|
|
172 |
*
|
|
173 |
* @return the calendar system type - 'iso8601'
|
|
174 |
* @see #getId()
|
|
175 |
*/
|
|
176 |
@Override
|
|
177 |
public String getCalendarType() {
|
|
178 |
return "iso8601";
|
|
179 |
}
|
|
180 |
|
|
181 |
//-----------------------------------------------------------------------
|
|
182 |
/**
|
|
183 |
* Obtains an ISO local date from the era, year-of-era, month-of-year
|
|
184 |
* and day-of-month fields.
|
|
185 |
*
|
|
186 |
* @param era the ISO era, not null
|
|
187 |
* @param yearOfEra the ISO year-of-era
|
|
188 |
* @param month the ISO month-of-year
|
|
189 |
* @param dayOfMonth the ISO day-of-month
|
|
190 |
* @return the ISO local date, not null
|
|
191 |
* @throws DateTimeException if unable to create the date
|
|
192 |
*/
|
|
193 |
@Override // override with covariant return type
|
|
194 |
public LocalDate date(Era era, int yearOfEra, int month, int dayOfMonth) {
|
|
195 |
return date(prolepticYear(era, yearOfEra), month, dayOfMonth);
|
|
196 |
}
|
|
197 |
|
|
198 |
/**
|
|
199 |
* Obtains an ISO local date from the proleptic-year, month-of-year
|
|
200 |
* and day-of-month fields.
|
|
201 |
* <p>
|
|
202 |
* This is equivalent to {@link LocalDate#of(int, int, int)}.
|
|
203 |
*
|
|
204 |
* @param prolepticYear the ISO proleptic-year
|
|
205 |
* @param month the ISO month-of-year
|
|
206 |
* @param dayOfMonth the ISO day-of-month
|
|
207 |
* @return the ISO local date, not null
|
|
208 |
* @throws DateTimeException if unable to create the date
|
|
209 |
*/
|
|
210 |
@Override // override with covariant return type
|
|
211 |
public LocalDate date(int prolepticYear, int month, int dayOfMonth) {
|
|
212 |
return LocalDate.of(prolepticYear, month, dayOfMonth);
|
|
213 |
}
|
|
214 |
|
|
215 |
/**
|
|
216 |
* Obtains an ISO local date from the era, year-of-era and day-of-year fields.
|
|
217 |
*
|
|
218 |
* @param era the ISO era, not null
|
|
219 |
* @param yearOfEra the ISO year-of-era
|
|
220 |
* @param dayOfYear the ISO day-of-year
|
|
221 |
* @return the ISO local date, not null
|
|
222 |
* @throws DateTimeException if unable to create the date
|
|
223 |
*/
|
|
224 |
@Override // override with covariant return type
|
|
225 |
public LocalDate dateYearDay(Era era, int yearOfEra, int dayOfYear) {
|
|
226 |
return dateYearDay(prolepticYear(era, yearOfEra), dayOfYear);
|
|
227 |
}
|
|
228 |
|
|
229 |
/**
|
|
230 |
* Obtains an ISO local date from the proleptic-year and day-of-year fields.
|
|
231 |
* <p>
|
|
232 |
* This is equivalent to {@link LocalDate#ofYearDay(int, int)}.
|
|
233 |
*
|
|
234 |
* @param prolepticYear the ISO proleptic-year
|
|
235 |
* @param dayOfYear the ISO day-of-year
|
|
236 |
* @return the ISO local date, not null
|
|
237 |
* @throws DateTimeException if unable to create the date
|
|
238 |
*/
|
|
239 |
@Override // override with covariant return type
|
|
240 |
public LocalDate dateYearDay(int prolepticYear, int dayOfYear) {
|
|
241 |
return LocalDate.ofYearDay(prolepticYear, dayOfYear);
|
|
242 |
}
|
|
243 |
|
|
244 |
//-----------------------------------------------------------------------
|
|
245 |
/**
|
|
246 |
* Obtains an ISO local date from another date-time object.
|
|
247 |
* <p>
|
|
248 |
* This is equivalent to {@link LocalDate#from(TemporalAccessor)}.
|
|
249 |
*
|
|
250 |
* @param temporal the date-time object to convert, not null
|
|
251 |
* @return the ISO local date, not null
|
|
252 |
* @throws DateTimeException if unable to create the date
|
|
253 |
*/
|
|
254 |
@Override // override with covariant return type
|
|
255 |
public LocalDate date(TemporalAccessor temporal) {
|
|
256 |
return LocalDate.from(temporal);
|
|
257 |
}
|
|
258 |
|
|
259 |
/**
|
|
260 |
* Obtains an ISO local date-time from another date-time object.
|
|
261 |
* <p>
|
|
262 |
* This is equivalent to {@link LocalDateTime#from(TemporalAccessor)}.
|
|
263 |
*
|
|
264 |
* @param temporal the date-time object to convert, not null
|
|
265 |
* @return the ISO local date-time, not null
|
|
266 |
* @throws DateTimeException if unable to create the date-time
|
|
267 |
*/
|
|
268 |
@Override // override with covariant return type
|
|
269 |
public LocalDateTime localDateTime(TemporalAccessor temporal) {
|
|
270 |
return LocalDateTime.from(temporal);
|
|
271 |
}
|
|
272 |
|
|
273 |
/**
|
|
274 |
* Obtains an ISO zoned date-time from another date-time object.
|
|
275 |
* <p>
|
|
276 |
* This is equivalent to {@link ZonedDateTime#from(TemporalAccessor)}.
|
|
277 |
*
|
|
278 |
* @param temporal the date-time object to convert, not null
|
|
279 |
* @return the ISO zoned date-time, not null
|
|
280 |
* @throws DateTimeException if unable to create the date-time
|
|
281 |
*/
|
|
282 |
@Override // override with covariant return type
|
|
283 |
public ZonedDateTime zonedDateTime(TemporalAccessor temporal) {
|
|
284 |
return ZonedDateTime.from(temporal);
|
|
285 |
}
|
|
286 |
|
|
287 |
/**
|
|
288 |
* Obtains an ISO zoned date-time in this chronology from an {@code Instant}.
|
|
289 |
* <p>
|
|
290 |
* This is equivalent to {@link ZonedDateTime#ofInstant(Instant, ZoneId)}.
|
|
291 |
*
|
|
292 |
* @param instant the instant to create the date-time from, not null
|
|
293 |
* @param zone the time-zone, not null
|
|
294 |
* @return the zoned date-time, not null
|
|
295 |
* @throws DateTimeException if the result exceeds the supported range
|
|
296 |
*/
|
|
297 |
@Override
|
|
298 |
public ZonedDateTime zonedDateTime(Instant instant, ZoneId zone) {
|
|
299 |
return ZonedDateTime.ofInstant(instant, zone);
|
|
300 |
}
|
|
301 |
|
|
302 |
//-----------------------------------------------------------------------
|
|
303 |
/**
|
|
304 |
* Obtains the current ISO local date from the system clock in the default time-zone.
|
|
305 |
* <p>
|
|
306 |
* This will query the {@link Clock#systemDefaultZone() system clock} in the default
|
|
307 |
* time-zone to obtain the current date.
|
|
308 |
* <p>
|
|
309 |
* Using this method will prevent the ability to use an alternate clock for testing
|
|
310 |
* because the clock is hard-coded.
|
|
311 |
*
|
|
312 |
* @return the current ISO local date using the system clock and default time-zone, not null
|
|
313 |
* @throws DateTimeException if unable to create the date
|
|
314 |
*/
|
|
315 |
@Override // override with covariant return type
|
|
316 |
public LocalDate dateNow() {
|
|
317 |
return dateNow(Clock.systemDefaultZone());
|
|
318 |
}
|
|
319 |
|
|
320 |
/**
|
|
321 |
* Obtains the current ISO local date from the system clock in the specified time-zone.
|
|
322 |
* <p>
|
|
323 |
* This will query the {@link Clock#system(ZoneId) system clock} to obtain the current date.
|
|
324 |
* Specifying the time-zone avoids dependence on the default time-zone.
|
|
325 |
* <p>
|
|
326 |
* Using this method will prevent the ability to use an alternate clock for testing
|
|
327 |
* because the clock is hard-coded.
|
|
328 |
*
|
|
329 |
* @return the current ISO local date using the system clock, not null
|
|
330 |
* @throws DateTimeException if unable to create the date
|
|
331 |
*/
|
|
332 |
@Override // override with covariant return type
|
|
333 |
public LocalDate dateNow(ZoneId zone) {
|
|
334 |
return dateNow(Clock.system(zone));
|
|
335 |
}
|
|
336 |
|
|
337 |
/**
|
|
338 |
* Obtains the current ISO local date from the specified clock.
|
|
339 |
* <p>
|
|
340 |
* This will query the specified clock to obtain the current date - today.
|
|
341 |
* Using this method allows the use of an alternate clock for testing.
|
|
342 |
* The alternate clock may be introduced using {@link Clock dependency injection}.
|
|
343 |
*
|
|
344 |
* @param clock the clock to use, not null
|
|
345 |
* @return the current ISO local date, not null
|
|
346 |
* @throws DateTimeException if unable to create the date
|
|
347 |
*/
|
|
348 |
@Override // override with covariant return type
|
|
349 |
public LocalDate dateNow(Clock clock) {
|
|
350 |
Objects.requireNonNull(clock, "clock");
|
|
351 |
return date(LocalDate.now(clock));
|
|
352 |
}
|
|
353 |
|
|
354 |
//-----------------------------------------------------------------------
|
|
355 |
/**
|
|
356 |
* Checks if the year is a leap year, according to the ISO proleptic
|
|
357 |
* calendar system rules.
|
|
358 |
* <p>
|
|
359 |
* This method applies the current rules for leap years across the whole time-line.
|
|
360 |
* In general, a year is a leap year if it is divisible by four without
|
|
361 |
* remainder. However, years divisible by 100, are not leap years, with
|
|
362 |
* the exception of years divisible by 400 which are.
|
|
363 |
* <p>
|
|
364 |
* For example, 1904 is a leap year it is divisible by 4.
|
|
365 |
* 1900 was not a leap year as it is divisible by 100, however 2000 was a
|
|
366 |
* leap year as it is divisible by 400.
|
|
367 |
* <p>
|
|
368 |
* The calculation is proleptic - applying the same rules into the far future and far past.
|
|
369 |
* This is historically inaccurate, but is correct for the ISO-8601 standard.
|
|
370 |
*
|
|
371 |
* @param prolepticYear the ISO proleptic year to check
|
|
372 |
* @return true if the year is leap, false otherwise
|
|
373 |
*/
|
|
374 |
@Override
|
|
375 |
public boolean isLeapYear(long prolepticYear) {
|
|
376 |
return ((prolepticYear & 3) == 0) && ((prolepticYear % 100) != 0 || (prolepticYear % 400) == 0);
|
|
377 |
}
|
|
378 |
|
|
379 |
@Override
|
|
380 |
public int prolepticYear(Era era, int yearOfEra) {
|
|
381 |
if (era instanceof IsoEra == false) {
|
|
382 |
throw new DateTimeException("Era must be IsoEra");
|
|
383 |
}
|
|
384 |
return (era == IsoEra.CE ? yearOfEra : 1 - yearOfEra);
|
|
385 |
}
|
|
386 |
|
|
387 |
@Override
|
|
388 |
public Era eraOf(int eraValue) {
|
|
389 |
return IsoEra.of(eraValue);
|
|
390 |
}
|
|
391 |
|
|
392 |
@Override
|
|
393 |
public List<Era> eras() {
|
|
394 |
return Arrays.<Era>asList(IsoEra.values());
|
|
395 |
}
|
|
396 |
|
|
397 |
//-----------------------------------------------------------------------
|
|
398 |
@Override
|
|
399 |
public ValueRange range(ChronoField field) {
|
|
400 |
return field.range();
|
|
401 |
}
|
|
402 |
|
|
403 |
}
|