< prev index next >

src/java.base/share/classes/java/time/chrono/IsoChronology.java

Print this page


   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      * formed using the specified year, month, day, hour, minute, second and zoneOffset.
 274      *
 275      * @param prolepticYear  the year to represent, from MIN_YEAR to MAX_YEAR
 276      * @param month  the month-of-year to represent, from 1 to 12
 277      * @param dayOfMonth  the day-of-month to represent, from 1 to 31
 278      * @param hour  the hour-of-day to represent, from 0 to 23
 279      * @param minute  the minute-of-hour to represent, from 0 to 59
 280      * @param second  the second-of-minute to represent, from 0 to 59
 281      * @param zoneOffset the zone offset, not null
 282      * @return the number of seconds relative to 1970-01-01T00:00:00Z, may be negative
 283      * @throws DateTimeException if the value of any field is out of range,
 284      *         or if the day-of-month is invalid for the month-year
 285      */
 286      @Override
 287      public long epochSecond(int prolepticYear, int month, int dayOfMonth,
 288                              int hour, int minute, int second, ZoneOffset zoneOffset) {
 289         YEAR.checkValidValue(prolepticYear);
 290         MONTH_OF_YEAR.checkValidValue(month);
 291         DAY_OF_MONTH.checkValidValue(dayOfMonth);
 292         HOUR_OF_DAY.checkValidValue(hour);
 293         MINUTE_OF_HOUR.checkValidValue(minute);
 294         SECOND_OF_MINUTE.checkValidValue(second);
 295         Objects.requireNonNull(zoneOffset, "zoneOffset");
 296         if (dayOfMonth > 28) {
 297             int dom = numberOfDaysOfMonth(prolepticYear, month);
 298             if (dayOfMonth > dom) {
 299                 if (dayOfMonth == 29) {
 300                     throw new DateTimeException("Invalid date 'February 29' as '" + prolepticYear + "' is not a leap year");
 301                 } else {
 302                     throw new DateTimeException("Invalid date '" + Month.of(month).name() + " " + dayOfMonth + "'");
 303                 }
 304             }
 305         }
 306 
 307         long totalDays = 0;
 308         int timeinSec = 0;
 309         totalDays += 365L * prolepticYear;
 310         long DAYS_0000_TO_1970 = (146097 * 5L) - (30L * 365L + 7L); // taken from LocalDate
 311         if (prolepticYear >= 0) {
 312             totalDays += (prolepticYear + 3L) / 4 - (prolepticYear + 99L) / 100 + (prolepticYear + 399L) / 400;
 313         } else {
 314             totalDays -= prolepticYear / -4 - prolepticYear / -100 + prolepticYear / -400;
 315         }
 316         totalDays += (367 * month - 362) / 12;
 317         totalDays += dayOfMonth - 1;
 318         if (month > 2) {
 319             totalDays--;
 320             if (IsoChronology.INSTANCE.isLeapYear(prolepticYear) == false) {
 321                 totalDays--;
 322             }
 323         }
 324         totalDays -= DAYS_0000_TO_1970;
 325         timeinSec = (hour * 60 + minute ) * 60 + second;
 326         return Math.addExact(Math.multiplyExact(totalDays, 86400L), timeinSec - zoneOffset.getTotalSeconds());
 327      }
 328 
 329     /**
 330      * Gets the number of days for the given month in the given year.
 331      *
 332      * @param year the year to represent, from MIN_YEAR to MAX_YEAR
 333      * @param month the month-of-year to represent, from 1 to 12
 334      * @return the number of days for the given month in the given year
 335      */
 336     private int numberOfDaysOfMonth(int year, int month) {
 337         int dom;
 338         switch (month) {
 339             case 2:
 340                 dom = (IsoChronology.INSTANCE.isLeapYear(year) ? 29 : 28);
 341                 break;
 342             case 4:
 343             case 6:
 344             case 9:
 345             case 11:
 346                 dom = 30;
 347                 break;
 348             default:
 349                 dom = 31;
 350                 break;
 351         }
 352         return dom;
 353     }
 354 
 355     //-----------------------------------------------------------------------
 356     /**
 357      * Gets the number of seconds from the epoch of 1970-01-01T00:00:00Z
 358      * formed using the specified era, yearOfEra, month, day, hour, minute, second and zoneOffset.
 359      *
 360      * @param era  the ISO era, not null
 361      * @param yearOfEra  the ISO year-of-era
 362      * @param month  the ISO month-of-year
 363      * @param dayOfMonth  the ISO day-of-month
 364      * @param hour  the hour-of-day to represent, from 0 to 23
 365      * @param minute  the minute-of-hour to represent, from 0 to 59
 366      * @param second  the second-of-minute to represent, from 0 to 59
 367      * @param zoneOffset the zone offset, not null
 368      * @return the number of seconds relative to 1970-01-01T00:00:00Z, may be negative
 369      * @throws DateTimeException if the value of any field is out of range,
 370      *         or if the day-of-month is invalid for the month-year
 371      */
 372      @Override
 373      public long epochSecond(Era era,
 374                              int yearOfEra, int month, int dayOfMonth,
 375                              int hour, int minute, int second, ZoneOffset zoneOffset) {
 376          Objects.requireNonNull(era, "era");
 377          if (era.getValue() == 0) {
 378              yearOfEra = 1 - yearOfEra;
 379          }
 380          return epochSecond(yearOfEra, month, dayOfMonth, hour, minute, second, zoneOffset);
 381      }
 382 
 383     /**
 384      * Obtains an ISO local date-time from another date-time object.
 385      * <p>
 386      * This is equivalent to {@link LocalDateTime#from(TemporalAccessor)}.
 387      *
 388      * @param temporal  the date-time object to convert, not null
 389      * @return the ISO local date-time, not null
 390      * @throws DateTimeException if unable to create the date-time
 391      */
 392     @Override  // override with covariant return type
 393     public LocalDateTime localDateTime(TemporalAccessor temporal) {
 394         return LocalDateTime.from(temporal);
 395     }
 396 
 397     /**
 398      * Obtains an ISO zoned date-time from another date-time object.
 399      * <p>
 400      * This is equivalent to {@link ZonedDateTime#from(TemporalAccessor)}.
 401      *
 402      * @param temporal  the date-time object to convert, not null


< prev index next >