Path: blob/master/src/java.base/share/classes/java/time/package-info.java
41152 views
/*1* Copyright (c) 2012, 2018, 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* The main API for dates, times, instants, and durations.65* </p>66* <p>67* The classes defined here represent the principle date-time concepts,68* including instants, durations, dates, times, time-zones and periods.69* They are based on the ISO calendar system, which is the <i>de facto</i> world70* calendar following the proleptic Gregorian rules.71* All the classes are immutable and thread-safe.72* </p>73* <p>74* Each date time instance is composed of fields that are conveniently75* made available by the APIs. For lower level access to the fields refer76* to the {@code java.time.temporal} package.77* Each class includes support for printing and parsing all manner of dates and times.78* Refer to the {@code java.time.format} package for customization options.79* </p>80* <p>81* The {@code java.time.chrono} package contains the calendar neutral API82* {@link java.time.chrono.ChronoLocalDate ChronoLocalDate},83* {@link java.time.chrono.ChronoLocalDateTime ChronoLocalDateTime},84* {@link java.time.chrono.ChronoZonedDateTime ChronoZonedDateTime} and85* {@link java.time.chrono.Era Era}.86* This is intended for use by applications that need to use localized calendars.87* It is recommended that applications use the ISO-8601 date and time classes from88* this package across system boundaries, such as to the database or across the network.89* The calendar neutral API should be reserved for interactions with users.90* </p>91*92* <h2>Dates and Times</h2>93* <p>94* {@link java.time.Instant} is essentially a numeric timestamp.95* The current Instant can be retrieved from a {@link java.time.Clock}.96* This is useful for logging and persistence of a point in time97* and has in the past been associated with storing the result98* from {@link java.lang.System#currentTimeMillis()}.99* </p>100* <p>101* {@link java.time.LocalDate} stores a date without a time.102* This stores a date like '2010-12-03' and could be used to store a birthday.103* </p>104* <p>105* {@link java.time.LocalTime} stores a time without a date.106* This stores a time like '11:30' and could be used to store an opening or closing time.107* </p>108* <p>109* {@link java.time.LocalDateTime} stores a date and time.110* This stores a date-time like '2010-12-03T11:30'.111* </p>112* <p>113* {@link java.time.ZonedDateTime} stores a date and time with a time-zone.114* This is useful if you want to perform accurate calculations of115* dates and times taking into account the {@link java.time.ZoneId}, such as 'Europe/Paris'.116* Where possible, it is recommended to use a simpler class without a time-zone.117* The widespread use of time-zones tends to add considerable complexity to an application.118* </p>119*120* <h2>Duration and Period</h2>121* <p>122* Beyond dates and times, the API also allows the storage of periods and durations of time.123* A {@link java.time.Duration} is a simple measure of time along the time-line in nanoseconds.124* A {@link java.time.Period} expresses an amount of time in units meaningful125* to humans, such as years or days.126* </p>127*128* <h2>Additional value types</h2>129* <p>130* {@link java.time.Month} stores a month on its own.131* This stores a single month-of-year in isolation, such as 'DECEMBER'.132* </p>133* <p>134* {@link java.time.DayOfWeek} stores a day-of-week on its own.135* This stores a single day-of-week in isolation, such as 'TUESDAY'.136* </p>137* <p>138* {@link java.time.Year} stores a year on its own.139* This stores a single year in isolation, such as '2010'.140* </p>141* <p>142* {@link java.time.YearMonth} stores a year and month without a day or time.143* This stores a year and month, such as '2010-12' and could be used for a credit card expiry.144* </p>145* <p>146* {@link java.time.MonthDay} stores a month and day without a year or time.147* This stores a month and day-of-month, such as '--12-03' and148* could be used to store an annual event like a birthday without storing the year.149* </p>150* <p>151* {@link java.time.OffsetTime} stores a time and offset from UTC without a date.152* This stores a date like '11:30+01:00'.153* The {@link java.time.ZoneOffset ZoneOffset} is of the form '+01:00'.154* </p>155* <p>156* {@link java.time.OffsetDateTime} stores a date and time and offset from UTC.157* This stores a date-time like '2010-12-03T11:30+01:00'.158* This is sometimes found in XML messages and other forms of persistence,159* but contains less information than a full time-zone.160* </p>161*162* <h2>Package specification</h2>163* <p>164* Unless otherwise noted, passing a null argument to a constructor or method in any class or interface165* in this package will cause a {@link java.lang.NullPointerException NullPointerException} to be thrown.166* The Javadoc "@param" definition is used to summarise the null-behavior.167* The "@throws {@link java.lang.NullPointerException}" is not explicitly documented in each method.168* </p>169* <p>170* All calculations should check for numeric overflow and throw either an {@link java.lang.ArithmeticException}171* or a {@link java.time.DateTimeException}.172* </p>173*174* <h2>Design notes (non normative)</h2>175* <p>176* The API has been designed to reject null early and to be clear about this behavior.177* A key exception is any method that takes an object and returns a boolean, for the purpose178* of checking or validating, will generally return false for null.179* </p>180* <p>181* The API is designed to be type-safe where reasonable in the main high-level API.182* Thus, there are separate classes for the distinct concepts of date, time and date-time,183* plus variants for offset and time-zone.184* This can seem like a lot of classes, but most applications can begin with just five date/time types.185* <ul>186* <li>{@link java.time.Instant} - a timestamp</li>187* <li>{@link java.time.LocalDate} - a date without a time, or any reference to an offset or time-zone</li>188* <li>{@link java.time.LocalTime} - a time without a date, or any reference to an offset or time-zone</li>189* <li>{@link java.time.LocalDateTime} - combines date and time, but still without any offset or time-zone</li>190* <li>{@link java.time.ZonedDateTime} - a "full" date-time with time-zone and resolved offset from UTC/Greenwich</li>191* </ul>192* <p>193* {@code Instant} is the closest equivalent class to {@code java.util.Date}.194* {@code ZonedDateTime} is the closest equivalent class to {@code java.util.GregorianCalendar}.195* </p>196* <p>197* Where possible, applications should use {@code LocalDate}, {@code LocalTime} and {@code LocalDateTime}198* to better model the domain. For example, a birthday should be stored in a code {@code LocalDate}.199* Bear in mind that any use of a {@linkplain java.time.ZoneId time-zone}, such as 'Europe/Paris', adds200* considerable complexity to a calculation.201* Many applications can be written only using {@code LocalDate}, {@code LocalTime} and {@code Instant},202* with the time-zone added at the user interface (UI) layer.203* </p>204* <p>205* The offset-based date-time types {@code OffsetTime} and {@code OffsetDateTime},206* are intended primarily for use with network protocols and database access.207* For example, most databases cannot automatically store a time-zone like 'Europe/Paris', but208* they can store an offset like '+02:00'.209* </p>210* <p>211* Classes are also provided for the most important sub-parts of a date, including {@code Month},212* {@code DayOfWeek}, {@code Year}, {@code YearMonth} and {@code MonthDay}.213* These can be used to model more complex date-time concepts.214* For example, {@code YearMonth} is useful for representing a credit card expiry.215* </p>216* <p>217* Note that while there are a large number of classes representing different aspects of dates,218* there are relatively few dealing with different aspects of time.219* Following type-safety to its logical conclusion would have resulted in classes for220* hour-minute, hour-minute-second and hour-minute-second-nanosecond.221* While logically pure, this was not a practical option as it would have almost tripled the222* number of classes due to the combinations of date and time.223* Thus, {@code LocalTime} is used for all precisions of time, with zeroes used to imply lower precision.224* </p>225* <p>226* Following full type-safety to its ultimate conclusion might also argue for a separate class227* for each field in date-time, such as a class for HourOfDay and another for DayOfMonth.228* This approach was tried, but was excessively complicated in the Java language, lacking usability.229* A similar problem occurs with periods.230* There is a case for a separate class for each period unit, such as a type for Years and a type for Minutes.231* However, this yields a lot of classes and a problem of type conversion.232* Thus, the set of date-time types provided is a compromise between purity and practicality.233* </p>234* <p>235* The API has a relatively large surface area in terms of number of methods.236* This is made manageable through the use of consistent method prefixes.237* <ul>238* <li>{@code of} - static factory method</li>239* <li>{@code parse} - static factory method focussed on parsing</li>240* <li>{@code get} - gets the value of something</li>241* <li>{@code is} - checks if something is true</li>242* <li>{@code with} - the immutable equivalent of a setter</li>243* <li>{@code plus} - adds an amount to an object</li>244* <li>{@code minus} - subtracts an amount from an object</li>245* <li>{@code to} - converts this object to another type</li>246* <li>{@code at} - combines this object with another, such as {@code date.atTime(time)}</li>247* </ul>248* <p>249* Multiple calendar systems is an awkward addition to the design challenges.250* The first principle is that most users want the standard ISO calendar system.251* As such, the main classes are ISO-only. The second principle is that most of those that want a252* non-ISO calendar system want it for user interaction, thus it is a UI localization issue.253* As such, date and time objects should be held as ISO objects in the data model and persistent254* storage, only being converted to and from a local calendar for display.255* The calendar system would be stored separately in the user preferences.256* </p>257* <p>258* There are, however, some limited use cases where users believe they need to store and use259* dates in arbitrary calendar systems throughout the application.260* This is supported by {@link java.time.chrono.ChronoLocalDate}, however it is vital to read261* all the associated warnings in the Javadoc of that interface before using it.262* In summary, applications that require general interoperation between multiple calendar systems263* typically need to be written in a very different way to those only using the ISO calendar,264* thus most applications should just use ISO and avoid {@code ChronoLocalDate}.265* </p>266* <p>267* The API is also designed for user extensibility, as there are many ways of calculating time.268* The {@linkplain java.time.temporal.TemporalField field} and {@linkplain java.time.temporal.TemporalUnit unit}269* API, accessed via {@link java.time.temporal.TemporalAccessor TemporalAccessor} and270* {@link java.time.temporal.Temporal Temporal} provide considerable flexibility to applications.271* In addition, the {@link java.time.temporal.TemporalQuery TemporalQuery} and272* {@link java.time.temporal.TemporalAdjuster TemporalAdjuster} interfaces provide day-to-day273* power, allowing code to read close to business requirements:274* </p>275* <pre>276* LocalDate customerBirthday = customer.loadBirthdayFromDatabase();277* LocalDate today = LocalDate.now();278* if (customerBirthday.equals(today)) {279* LocalDate specialOfferExpiryDate = today.plusWeeks(2).with(next(FRIDAY));280* customer.sendBirthdaySpecialOffer(specialOfferExpiryDate);281* }282*283* </pre>284*285* @since 1.8286*/287package java.time;288289290