Path: blob/master/src/java.base/share/classes/java/time/chrono/package-info.java
41159 views
/*1* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* This file is available under and governed by the GNU General Public27* License version 2 only, as published by the Free Software Foundation.28* However, the following notice accompanied the original version of this29* file:30*31* Copyright (c) 2012, Stephen Colebourne & Michael Nascimento Santos32*33* All rights reserved.34*35* Redistribution and use in source and binary forms, with or without36* modification, are permitted provided that the following conditions are met:37*38* * Redistributions of source code must retain the above copyright notice,39* this list of conditions and the following disclaimer.40*41* * Redistributions in binary form must reproduce the above copyright notice,42* this list of conditions and the following disclaimer in the documentation43* and/or other materials provided with the distribution.44*45* * Neither the name of JSR-310 nor the names of its contributors46* may be used to endorse or promote products derived from this software47* without specific prior written permission.48*49* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS50* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT51* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR52* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR53* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,54* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,55* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR56* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF57* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING58* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS59* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.60*/6162/**63* <p>64* Generic API for calendar systems other than the default ISO.65* </p>66* <p>67* The main API is based around the calendar system defined in ISO-8601.68* However, there are other calendar systems, and this package provides basic support for them.69* The alternate calendars are provided in the {@link java.time.chrono} package.70* </p>71* <p>72* A calendar system is defined by the {@link java.time.chrono.Chronology} interface,73* while a date in a calendar system is defined by the {@link java.time.chrono.ChronoLocalDate} interface.74* </p>75* <p>76* It is intended that applications use the main API whenever possible, including code to read and write77* from a persistent data store, such as a database, and to send dates and times across a network.78* The "chrono" classes are then used at the user interface level to deal with localized input/output.79* See {@link java.time.chrono.ChronoLocalDate ChronoLocalDate}80* for a full discussion of the issues.81* </p>82* <p>83* Using non-ISO calendar systems in an application introduces significant extra complexity.84* Ensure that the warnings and recommendations in {@code ChronoLocalDate} have been read before85* working with the "chrono" interfaces.86* </p>87* <p>88* The supported calendar systems includes:89* </p>90* <ul>91* <li>{@link java.time.chrono.HijrahChronology Hijrah calendar}</li>92* <li>{@link java.time.chrono.JapaneseChronology Japanese calendar}</li>93* <li>{@link java.time.chrono.MinguoChronology Minguo calendar}</li>94* <li>{@link java.time.chrono.ThaiBuddhistChronology Thai Buddhist calendar}</li>95* </ul>96*97* <h2>Example</h2>98* <p>99* This example lists today's date for all of the available calendars.100* </p>101* <pre>102* // Enumerate the list of available calendars and print today's date for each.103* Set<Chronology> chronos = Chronology.getAvailableChronologies();104* for (Chronology chrono : chronos) {105* ChronoLocalDate date = chrono.dateNow();106* System.out.printf(" %20s: %s%n", chrono.getId(), date.toString());107* }108* </pre>109*110* <p>111* This example creates and uses a date in a named non-ISO calendar system.112* </p>113* <pre>114* // Print the Thai Buddhist date115* ChronoLocalDate now1 = Chronology.of("ThaiBuddhist").dateNow();116* int day = now1.get(ChronoField.DAY_OF_MONTH);117* int dow = now1.get(ChronoField.DAY_OF_WEEK);118* int month = now1.get(ChronoField.MONTH_OF_YEAR);119* int year = now1.get(ChronoField.YEAR);120* System.out.printf(" Today is %s %s %d-%s-%d%n", now1.getChronology().getId(),121* dow, day, month, year);122* // Print today's date and the last day of the year for the Thai Buddhist Calendar.123* ChronoLocalDate first = now1124* .with(ChronoField.DAY_OF_MONTH, 1)125* .with(ChronoField.MONTH_OF_YEAR, 1);126* ChronoLocalDate last = first127* .plus(1, ChronoUnit.YEARS)128* .minus(1, ChronoUnit.DAYS);129* System.out.printf(" %s: 1st of year: %s; end of year: %s%n", last.getChronology().getId(),130* first, last);131* </pre>132*133* <p>134* This example creates and uses a date in a specific ThaiBuddhist calendar system.135* </p>136* <pre>137* // Print the Thai Buddhist date138* ThaiBuddhistDate now1 = ThaiBuddhistDate.now();139* int day = now1.get(ChronoField.DAY_OF_MONTH);140* int dow = now1.get(ChronoField.DAY_OF_WEEK);141* int month = now1.get(ChronoField.MONTH_OF_YEAR);142* int year = now1.get(ChronoField.YEAR);143* System.out.printf(" Today is %s %s %d-%s-%d%n", now1.getChronology().getId(),144* dow, day, month, year);145*146* // Print today's date and the last day of the year for the Thai Buddhist Calendar.147* ThaiBuddhistDate first = now1148* .with(ChronoField.DAY_OF_MONTH, 1)149* .with(ChronoField.MONTH_OF_YEAR, 1);150* ThaiBuddhistDate last = first151* .plus(1, ChronoUnit.YEARS)152* .minus(1, ChronoUnit.DAYS);153* System.out.printf(" %s: 1st of year: %s; end of year: %s%n", last.getChronology().getId(),154* first, last);155* </pre>156*157* <h2>Package specification</h2>158* <p>159* Unless otherwise noted, passing a null argument to a constructor or method in any class or interface160* in this package will cause a {@link java.lang.NullPointerException NullPointerException} to be thrown.161* The Javadoc "@param" definition is used to summarise the null-behavior.162* The "@throws {@link java.lang.NullPointerException}" is not explicitly documented in each method.163* </p>164* <p>165* All calculations should check for numeric overflow and throw either an {@link java.lang.ArithmeticException}166* or a {@link java.time.DateTimeException}.167* </p>168* @since 1.8169*/170package java.time.chrono;171172173