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
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.InvalidObjectException;
65 import static java.time.temporal.ChronoField.DAY_OF_MONTH;
66 import static java.time.temporal.ChronoField.ERA;
67 import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
68 import static java.time.temporal.ChronoField.PROLEPTIC_MONTH;
69 import static java.time.temporal.ChronoField.YEAR;
70 import static java.time.temporal.ChronoField.YEAR_OF_ERA;
71
72 import java.io.ObjectInputStream;
73 import java.io.Serializable;
74 import java.time.Clock;
75 import java.time.DateTimeException;
76 import java.time.Instant;
77 import java.time.LocalDate;
78 import java.time.LocalDateTime;
79 import java.time.Month;
80 import java.time.Period;
81 import java.time.Year;
82 import java.time.ZoneId;
83 import java.time.ZonedDateTime;
84 import java.time.format.ResolverStyle;
85 import java.time.temporal.ChronoField;
86 import java.time.temporal.TemporalAccessor;
87 import java.time.temporal.TemporalField;
88 import java.time.temporal.ValueRange;
89 import java.util.Arrays;
90 import java.util.List;
91 import java.util.Locale;
92 import java.util.Map;
93 import java.util.Objects;
94
95 /**
96 * The ISO calendar system.
97 * <p>
98 * This chronology defines the rules of the ISO calendar system.
99 * This calendar system is based on the ISO-8601 standard, which is the
100 * <i>de facto</i> world calendar.
101 * <p>
102 * The fields are defined as follows:
103 * <ul>
246 @Override // override with covariant return type
247 public LocalDate dateEpochDay(long epochDay) {
248 return LocalDate.ofEpochDay(epochDay);
249 }
250
251 //-----------------------------------------------------------------------
252 /**
253 * Obtains an ISO local date from another date-time object.
254 * <p>
255 * This is equivalent to {@link LocalDate#from(TemporalAccessor)}.
256 *
257 * @param temporal the date-time object to convert, not null
258 * @return the ISO local date, not null
259 * @throws DateTimeException if unable to create the date
260 */
261 @Override // override with covariant return type
262 public LocalDate date(TemporalAccessor temporal) {
263 return LocalDate.from(temporal);
264 }
265
266 /**
267 * Obtains an ISO local date-time from another date-time object.
268 * <p>
269 * This is equivalent to {@link LocalDateTime#from(TemporalAccessor)}.
270 *
271 * @param temporal the date-time object to convert, not null
272 * @return the ISO local date-time, not null
273 * @throws DateTimeException if unable to create the date-time
274 */
275 @Override // override with covariant return type
276 public LocalDateTime localDateTime(TemporalAccessor temporal) {
277 return LocalDateTime.from(temporal);
278 }
279
280 /**
281 * Obtains an ISO zoned date-time from another date-time object.
282 * <p>
283 * This is equivalent to {@link ZonedDateTime#from(TemporalAccessor)}.
284 *
285 * @param temporal the date-time object to convert, not null
|
1 /*
2 * Copyright (c) 2012, 2016, 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
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 static java.time.temporal.ChronoField.DAY_OF_MONTH;
65 import static java.time.temporal.ChronoField.ERA;
66 import static java.time.temporal.ChronoField.HOUR_OF_DAY;
67 import static java.time.temporal.ChronoField.MINUTE_OF_HOUR;
68 import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
69 import static java.time.temporal.ChronoField.PROLEPTIC_MONTH;
70 import static java.time.temporal.ChronoField.SECOND_OF_MINUTE;
71 import static java.time.temporal.ChronoField.YEAR;
72 import static java.time.temporal.ChronoField.YEAR_OF_ERA;
73
74 import java.io.InvalidObjectException;
75 import java.io.ObjectInputStream;
76 import java.io.Serializable;
77 import java.time.Clock;
78 import java.time.DateTimeException;
79 import java.time.Instant;
80 import java.time.LocalDate;
81 import java.time.LocalDateTime;
82 import java.time.Month;
83 import java.time.Period;
84 import java.time.Year;
85 import java.time.ZonedDateTime;
86 import java.time.ZoneId;
87 import java.time.ZoneOffset;
88 import java.time.format.ResolverStyle;
89 import java.time.temporal.ChronoField;
90 import java.time.temporal.TemporalAccessor;
91 import java.time.temporal.TemporalField;
92 import java.time.temporal.ValueRange;
93 import java.util.Arrays;
94 import java.util.List;
95 import java.util.Locale;
96 import java.util.Map;
97 import java.util.Objects;
98
99 /**
100 * The ISO calendar system.
101 * <p>
102 * This chronology defines the rules of the ISO calendar system.
103 * This calendar system is based on the ISO-8601 standard, which is the
104 * <i>de facto</i> world calendar.
105 * <p>
106 * The fields are defined as follows:
107 * <ul>
250 @Override // override with covariant return type
251 public LocalDate dateEpochDay(long epochDay) {
252 return LocalDate.ofEpochDay(epochDay);
253 }
254
255 //-----------------------------------------------------------------------
256 /**
257 * Obtains an ISO local date from another date-time object.
258 * <p>
259 * This is equivalent to {@link LocalDate#from(TemporalAccessor)}.
260 *
261 * @param temporal the date-time object to convert, not null
262 * @return the ISO local date, not null
263 * @throws DateTimeException if unable to create the date
264 */
265 @Override // override with covariant return type
266 public LocalDate date(TemporalAccessor temporal) {
267 return LocalDate.from(temporal);
268 }
269
270 //-----------------------------------------------------------------------
271 /**
272 * Gets the number of seconds from the epoch of 1970-01-01T00:00:00Z.
273 * <p>
274 * The number of seconds is calculated using the year,
275 * month, day-of-month, hour, minute, second, and zoneOffset.
276 *
277 * @param prolepticYear the year, from MIN_YEAR to MAX_YEAR
278 * @param month the month-of-year, from 1 to 12
279 * @param dayOfMonth the day-of-month, from 1 to 31
280 * @param hour the hour-of-day, from 0 to 23
281 * @param minute the minute-of-hour, from 0 to 59
282 * @param second the second-of-minute, from 0 to 59
283 * @param zoneOffset the zone offset, not null
284 * @return the number of seconds relative to 1970-01-01T00:00:00Z, may be negative
285 * @throws DateTimeException if the value of any field is out of range,
286 * or if the day-of-month is invalid for the month-of-year
287 * @since 9
288 */
289 @Override
290 public long epochSecond(int prolepticYear, int month, int dayOfMonth,
291 int hour, int minute, int second, ZoneOffset zoneOffset) {
292 YEAR.checkValidValue(prolepticYear);
293 MONTH_OF_YEAR.checkValidValue(month);
294 DAY_OF_MONTH.checkValidValue(dayOfMonth);
295 HOUR_OF_DAY.checkValidValue(hour);
296 MINUTE_OF_HOUR.checkValidValue(minute);
297 SECOND_OF_MINUTE.checkValidValue(second);
298 Objects.requireNonNull(zoneOffset, "zoneOffset");
299 if (dayOfMonth > 28) {
300 int dom = numberOfDaysOfMonth(prolepticYear, month);
301 if (dayOfMonth > dom) {
302 if (dayOfMonth == 29) {
303 throw new DateTimeException("Invalid date 'February 29' as '" + prolepticYear + "' is not a leap year");
304 } else {
305 throw new DateTimeException("Invalid date '" + Month.of(month).name() + " " + dayOfMonth + "'");
306 }
307 }
308 }
309
310 long totalDays = 0;
311 int timeinSec = 0;
312 totalDays += 365L * prolepticYear;
313 long DAYS_0000_TO_1970 = (146097 * 5L) - (30L * 365L + 7L); // taken from LocalDate
314 if (prolepticYear >= 0) {
315 totalDays += (prolepticYear + 3L) / 4 - (prolepticYear + 99L) / 100 + (prolepticYear + 399L) / 400;
316 } else {
317 totalDays -= prolepticYear / -4 - prolepticYear / -100 + prolepticYear / -400;
318 }
319 totalDays += (367 * month - 362) / 12;
320 totalDays += dayOfMonth - 1;
321 if (month > 2) {
322 totalDays--;
323 if (IsoChronology.INSTANCE.isLeapYear(prolepticYear) == false) {
324 totalDays--;
325 }
326 }
327 totalDays -= DAYS_0000_TO_1970;
328 timeinSec = (hour * 60 + minute ) * 60 + second;
329 return Math.addExact(Math.multiplyExact(totalDays, 86400L), timeinSec - zoneOffset.getTotalSeconds());
330 }
331
332 /**
333 * Gets the number of days for the given month in the given year.
334 *
335 * @param year the year to represent, from MIN_YEAR to MAX_YEAR
336 * @param month the month-of-year to represent, from 1 to 12
337 * @return the number of days for the given month in the given year
338 */
339 private int numberOfDaysOfMonth(int year, int month) {
340 int dom;
341 switch (month) {
342 case 2:
343 dom = (IsoChronology.INSTANCE.isLeapYear(year) ? 29 : 28);
344 break;
345 case 4:
346 case 6:
347 case 9:
348 case 11:
349 dom = 30;
350 break;
351 default:
352 dom = 31;
353 break;
354 }
355 return dom;
356 }
357
358
359 /**
360 * Obtains an ISO local date-time from another date-time object.
361 * <p>
362 * This is equivalent to {@link LocalDateTime#from(TemporalAccessor)}.
363 *
364 * @param temporal the date-time object to convert, not null
365 * @return the ISO local date-time, not null
366 * @throws DateTimeException if unable to create the date-time
367 */
368 @Override // override with covariant return type
369 public LocalDateTime localDateTime(TemporalAccessor temporal) {
370 return LocalDateTime.from(temporal);
371 }
372
373 /**
374 * Obtains an ISO zoned date-time from another date-time object.
375 * <p>
376 * This is equivalent to {@link ZonedDateTime#from(TemporalAccessor)}.
377 *
378 * @param temporal the date-time object to convert, not null
|