< prev index next >

src/java.base/share/classes/java/time/chrono/Chronology.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


  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.time.Clock;
  65 import java.time.DateTimeException;
  66 import java.time.Instant;
  67 import java.time.LocalDate;
  68 import java.time.LocalTime;
  69 import java.time.ZoneId;

  70 import java.time.format.DateTimeFormatterBuilder;
  71 import java.time.format.ResolverStyle;
  72 import java.time.format.TextStyle;
  73 import java.time.temporal.ChronoField;
  74 import java.time.temporal.TemporalAccessor;
  75 import java.time.temporal.TemporalField;
  76 import java.time.temporal.TemporalQueries;
  77 import java.time.temporal.TemporalQuery;
  78 import java.time.temporal.UnsupportedTemporalTypeException;
  79 import java.time.temporal.ValueRange;
  80 import java.util.List;
  81 import java.util.Locale;
  82 import java.util.Map;
  83 import java.util.Objects;
  84 import java.util.Set;
  85 
  86 /**
  87  * A calendar system, used to organize and identify dates.
  88  * <p>
  89  * The main date and time API is built on the ISO calendar system.


 695      * Normalization, addition and subtraction derive the number of months
 696      * in a year from the {@link #range(ChronoField)}. If the number of
 697      * months within a year is fixed, then the calculation approach for
 698      * addition, subtraction and normalization is slightly different.
 699      * <p>
 700      * If implementing an unusual calendar system that is not based on
 701      * years, months and days, or where you want direct control, then
 702      * the {@code ChronoPeriod} interface must be directly implemented.
 703      * <p>
 704      * The returned period is immutable and thread-safe.
 705      *
 706      * @param years  the number of years, may be negative
 707      * @param months  the number of years, may be negative
 708      * @param days  the number of years, may be negative
 709      * @return the period in terms of this chronology, not null
 710      */
 711     default ChronoPeriod period(int years, int months, int days) {
 712         return new ChronoPeriodImpl(this, years, months, days);
 713     }
 714 














































 715     //-----------------------------------------------------------------------
 716     /**
 717      * Compares this chronology to another chronology.
 718      * <p>
 719      * The comparison order first by the chronology ID string, then by any
 720      * additional information specific to the subclass.
 721      * It is "consistent with equals", as defined by {@link Comparable}.
 722      *
 723      * @param other  the other chronology to compare to, not null
 724      * @return the comparator value, negative if less, positive if greater
 725      */
 726     @Override
 727     int compareTo(Chronology other);
 728 
 729     /**
 730      * Checks if this chronology is equal to another chronology.
 731      * <p>
 732      * The comparison is based on the entire state of the object.
 733      *
 734      * @param obj  the object to check, null returns false


   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


  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.time.Clock;
  65 import java.time.DateTimeException;
  66 import java.time.Instant;
  67 import java.time.LocalDate;
  68 import java.time.LocalTime;
  69 import java.time.ZoneId;
  70 import java.time.ZoneOffset;
  71 import java.time.format.DateTimeFormatterBuilder;
  72 import java.time.format.ResolverStyle;
  73 import java.time.format.TextStyle;
  74 import java.time.temporal.ChronoField;
  75 import java.time.temporal.TemporalAccessor;
  76 import java.time.temporal.TemporalField;
  77 import java.time.temporal.TemporalQueries;
  78 import java.time.temporal.TemporalQuery;
  79 import java.time.temporal.UnsupportedTemporalTypeException;
  80 import java.time.temporal.ValueRange;
  81 import java.util.List;
  82 import java.util.Locale;
  83 import java.util.Map;
  84 import java.util.Objects;
  85 import java.util.Set;
  86 
  87 /**
  88  * A calendar system, used to organize and identify dates.
  89  * <p>
  90  * The main date and time API is built on the ISO calendar system.


 696      * Normalization, addition and subtraction derive the number of months
 697      * in a year from the {@link #range(ChronoField)}. If the number of
 698      * months within a year is fixed, then the calculation approach for
 699      * addition, subtraction and normalization is slightly different.
 700      * <p>
 701      * If implementing an unusual calendar system that is not based on
 702      * years, months and days, or where you want direct control, then
 703      * the {@code ChronoPeriod} interface must be directly implemented.
 704      * <p>
 705      * The returned period is immutable and thread-safe.
 706      *
 707      * @param years  the number of years, may be negative
 708      * @param months  the number of years, may be negative
 709      * @param days  the number of years, may be negative
 710      * @return the period in terms of this chronology, not null
 711      */
 712     default ChronoPeriod period(int years, int months, int days) {
 713         return new ChronoPeriodImpl(this, years, months, days);
 714     }
 715 
 716     //---------------------------------------------------------------------
 717 
 718     /**
 719      * Gets the number of seconds from the Java epoch of 1970-01-01T00:00:00Z.
 720      * <p>
 721      * Returns the number of seconds from the epoch of 1970-01-01T00:00:00Z
 722      * formed using given prolepticYear, month, dayOfMonth , hour, minute, second and zoneOffset.
 723      *
 724      * @param prolepticYear the chronology proleptic-year
 725      * @param month the chronology month-of-year
 726      * @param dayOfMonth the chronology day-of-month
 727      * @param hour the hour-of-day to represent, from 0 to 23
 728      * @param minute the minute-of-hour to represent, from 0 to 59
 729      * @param second the second-of-minute to represent, from 0 to 59
 730      * @param zoneOffset the zone offset, not null
 731      * @return the number of seconds relative to 1970-01-01T00:00:00Z, may be negative
 732      */
 733      public default long epochSecond(int prolepticYear, int month, int dayOfMonth,
 734                                      int hour, int minute, int second, ZoneOffset zoneOffset) {
 735         Objects.requireNonNull(zoneOffset, "zoneOffset");
 736         long daysInSec = Math.multiplyExact(date(prolepticYear, month, dayOfMonth).toEpochDay(), 86400);
 737         long timeinSec = (hour * 60 + minute) * 60 + second;
 738         return Math.addExact(daysInSec, timeinSec - zoneOffset.getTotalSeconds());
 739     }
 740 
 741     /**
 742      * Gets the number of seconds from the epoch of 1970-01-01T00:00:00Z.
 743      * <p>
 744      * The number of seconds is caluculated using given era, prolepticYear,
 745      * month, dayOfMonth , hour, minute, second and zoneOffset.
 746      *
 747      * @param era  the era of the correct type for the chronology, not null
 748      * @param yearOfEra the chronology year-of-era
 749      * @param month the chronology month-of-year
 750      * @param dayOfMonth the chronology day-of-month
 751      * @param hour the hour-of-day to represent, from 0 to 23
 752      * @param minute the minute-of-hour to represent, from 0 to 59
 753      * @param second the second-of-minute to represent, from 0 to 59
 754      * @param zoneOffset the zone offset, not null
 755      * @return the number of seconds relative to 1970-01-01T00:00:00Z, may be negative
 756      */
 757      public default long epochSecond(Era era, int yearOfEra, int month, int dayOfMonth,
 758                                      int hour, int minute, int second, ZoneOffset zoneOffset) {
 759         Objects.requireNonNull(era, "era");
 760         return epochSecond(prolepticYear(era, yearOfEra), month, dayOfMonth, hour, minute, second, zoneOffset);
 761     }
 762     //-----------------------------------------------------------------------
 763     /**
 764      * Compares this chronology to another chronology.
 765      * <p>
 766      * The comparison order first by the chronology ID string, then by any
 767      * additional information specific to the subclass.
 768      * It is "consistent with equals", as defined by {@link Comparable}.
 769      *
 770      * @param other  the other chronology to compare to, not null
 771      * @return the comparator value, negative if less, positive if greater
 772      */
 773     @Override
 774     int compareTo(Chronology other);
 775 
 776     /**
 777      * Checks if this chronology is equal to another chronology.
 778      * <p>
 779      * The comparison is based on the entire state of the object.
 780      *
 781      * @param obj  the object to check, null returns false


< prev index next >