Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/time/chrono/ChronoLocalDate.java
38918 views
/*1* Copyright (c) 2012, 2015, 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*/61package java.time.chrono;6263import static java.time.temporal.ChronoField.EPOCH_DAY;64import static java.time.temporal.ChronoField.ERA;65import static java.time.temporal.ChronoField.YEAR;66import static java.time.temporal.ChronoUnit.DAYS;6768import java.time.DateTimeException;69import java.time.LocalDate;70import java.time.LocalTime;71import java.time.format.DateTimeFormatter;72import java.time.temporal.ChronoField;73import java.time.temporal.ChronoUnit;74import java.time.temporal.Temporal;75import java.time.temporal.TemporalAccessor;76import java.time.temporal.TemporalAdjuster;77import java.time.temporal.TemporalAmount;78import java.time.temporal.TemporalField;79import java.time.temporal.TemporalQueries;80import java.time.temporal.TemporalQuery;81import java.time.temporal.TemporalUnit;82import java.time.temporal.UnsupportedTemporalTypeException;83import java.util.Comparator;84import java.util.Objects;8586/**87* A date without time-of-day or time-zone in an arbitrary chronology, intended88* for advanced globalization use cases.89* <p>90* <b>Most applications should declare method signatures, fields and variables91* as {@link LocalDate}, not this interface.</b>92* <p>93* A {@code ChronoLocalDate} is the abstract representation of a date where the94* {@code Chronology chronology}, or calendar system, is pluggable.95* The date is defined in terms of fields expressed by {@link TemporalField},96* where most common implementations are defined in {@link ChronoField}.97* The chronology defines how the calendar system operates and the meaning of98* the standard fields.99*100* <h3>When to use this interface</h3>101* The design of the API encourages the use of {@code LocalDate} rather than this102* interface, even in the case where the application needs to deal with multiple103* calendar systems.104* <p>105* This concept can seem surprising at first, as the natural way to globalize an106* application might initially appear to be to abstract the calendar system.107* However, as explored below, abstracting the calendar system is usually the wrong108* approach, resulting in logic errors and hard to find bugs.109* As such, it should be considered an application-wide architectural decision to choose110* to use this interface as opposed to {@code LocalDate}.111*112* <h3>Architectural issues to consider</h3>113* These are some of the points that must be considered before using this interface114* throughout an application.115* <p>116* 1) Applications using this interface, as opposed to using just {@code LocalDate},117* face a significantly higher probability of bugs. This is because the calendar system118* in use is not known at development time. A key cause of bugs is where the developer119* applies assumptions from their day-to-day knowledge of the ISO calendar system120* to code that is intended to deal with any arbitrary calendar system.121* The section below outlines how those assumptions can cause problems122* The primary mechanism for reducing this increased risk of bugs is a strong code review process.123* This should also be considered a extra cost in maintenance for the lifetime of the code.124* <p>125* 2) This interface does not enforce immutability of implementations.126* While the implementation notes indicate that all implementations must be immutable127* there is nothing in the code or type system to enforce this. Any method declared128* to accept a {@code ChronoLocalDate} could therefore be passed a poorly or129* maliciously written mutable implementation.130* <p>131* 3) Applications using this interface must consider the impact of eras.132* {@code LocalDate} shields users from the concept of eras, by ensuring that {@code getYear()}133* returns the proleptic year. That decision ensures that developers can think of134* {@code LocalDate} instances as consisting of three fields - year, month-of-year and day-of-month.135* By contrast, users of this interface must think of dates as consisting of four fields -136* era, year-of-era, month-of-year and day-of-month. The extra era field is frequently137* forgotten, yet it is of vital importance to dates in an arbitrary calendar system.138* For example, in the Japanese calendar system, the era represents the reign of an Emperor.139* Whenever one reign ends and another starts, the year-of-era is reset to one.140* <p>141* 4) The only agreed international standard for passing a date between two systems142* is the ISO-8601 standard which requires the ISO calendar system. Using this interface143* throughout the application will inevitably lead to the requirement to pass the date144* across a network or component boundary, requiring an application specific protocol or format.145* <p>146* 5) Long term persistence, such as a database, will almost always only accept dates in the147* ISO-8601 calendar system (or the related Julian-Gregorian). Passing around dates in other148* calendar systems increases the complications of interacting with persistence.149* <p>150* 6) Most of the time, passing a {@code ChronoLocalDate} throughout an application151* is unnecessary, as discussed in the last section below.152*153* <h3>False assumptions causing bugs in multi-calendar system code</h3>154* As indicated above, there are many issues to consider when try to use and manipulate a155* date in an arbitrary calendar system. These are some of the key issues.156* <p>157* Code that queries the day-of-month and assumes that the value will never be more than158* 31 is invalid. Some calendar systems have more than 31 days in some months.159* <p>160* Code that adds 12 months to a date and assumes that a year has been added is invalid.161* Some calendar systems have a different number of months, such as 13 in the Coptic or Ethiopic.162* <p>163* Code that adds one month to a date and assumes that the month-of-year value will increase164* by one or wrap to the next year is invalid. Some calendar systems have a variable number165* of months in a year, such as the Hebrew.166* <p>167* Code that adds one month, then adds a second one month and assumes that the day-of-month168* will remain close to its original value is invalid. Some calendar systems have a large difference169* between the length of the longest month and the length of the shortest month.170* For example, the Coptic or Ethiopic have 12 months of 30 days and 1 month of 5 days.171* <p>172* Code that adds seven days and assumes that a week has been added is invalid.173* Some calendar systems have weeks of other than seven days, such as the French Revolutionary.174* <p>175* Code that assumes that because the year of {@code date1} is greater than the year of {@code date2}176* then {@code date1} is after {@code date2} is invalid. This is invalid for all calendar systems177* when referring to the year-of-era, and especially untrue of the Japanese calendar system178* where the year-of-era restarts with the reign of every new Emperor.179* <p>180* Code that treats month-of-year one and day-of-month one as the start of the year is invalid.181* Not all calendar systems start the year when the month value is one.182* <p>183* In general, manipulating a date, and even querying a date, is wide open to bugs when the184* calendar system is unknown at development time. This is why it is essential that code using185* this interface is subjected to additional code reviews. It is also why an architectural186* decision to avoid this interface type is usually the correct one.187*188* <h3>Using LocalDate instead</h3>189* The primary alternative to using this interface throughout your application is as follows.190* <ul>191* <li>Declare all method signatures referring to dates in terms of {@code LocalDate}.192* <li>Either store the chronology (calendar system) in the user profile or lookup193* the chronology from the user locale194* <li>Convert the ISO {@code LocalDate} to and from the user's preferred calendar system during195* printing and parsing196* </ul>197* This approach treats the problem of globalized calendar systems as a localization issue198* and confines it to the UI layer. This approach is in keeping with other localization199* issues in the java platform.200* <p>201* As discussed above, performing calculations on a date where the rules of the calendar system202* are pluggable requires skill and is not recommended.203* Fortunately, the need to perform calculations on a date in an arbitrary calendar system204* is extremely rare. For example, it is highly unlikely that the business rules of a library205* book rental scheme will allow rentals to be for one month, where meaning of the month206* is dependent on the user's preferred calendar system.207* <p>208* A key use case for calculations on a date in an arbitrary calendar system is producing209* a month-by-month calendar for display and user interaction. Again, this is a UI issue,210* and use of this interface solely within a few methods of the UI layer may be justified.211* <p>212* In any other part of the system, where a date must be manipulated in a calendar system213* other than ISO, the use case will generally specify the calendar system to use.214* For example, an application may need to calculate the next Islamic or Hebrew holiday215* which may require manipulating the date.216* This kind of use case can be handled as follows:217* <ul>218* <li>start from the ISO {@code LocalDate} being passed to the method219* <li>convert the date to the alternate calendar system, which for this use case is known220* rather than arbitrary221* <li>perform the calculation222* <li>convert back to {@code LocalDate}223* </ul>224* Developers writing low-level frameworks or libraries should also avoid this interface.225* Instead, one of the two general purpose access interfaces should be used.226* Use {@link TemporalAccessor} if read-only access is required, or use {@link Temporal}227* if read-write access is required.228*229* @implSpec230* This interface must be implemented with care to ensure other classes operate correctly.231* All implementations that can be instantiated must be final, immutable and thread-safe.232* Subclasses should be Serializable wherever possible.233* <p>234* Additional calendar systems may be added to the system.235* See {@link Chronology} for more details.236*237* @since 1.8238*/239public interface ChronoLocalDate240extends Temporal, TemporalAdjuster, Comparable<ChronoLocalDate> {241242/**243* Gets a comparator that compares {@code ChronoLocalDate} in244* time-line order ignoring the chronology.245* <p>246* This comparator differs from the comparison in {@link #compareTo} in that it247* only compares the underlying date and not the chronology.248* This allows dates in different calendar systems to be compared based249* on the position of the date on the local time-line.250* The underlying comparison is equivalent to comparing the epoch-day.251*252* @return a comparator that compares in time-line order ignoring the chronology253* @see #isAfter254* @see #isBefore255* @see #isEqual256*/257static Comparator<ChronoLocalDate> timeLineOrder() {258return AbstractChronology.DATE_ORDER;259}260261//-----------------------------------------------------------------------262/**263* Obtains an instance of {@code ChronoLocalDate} from a temporal object.264* <p>265* This obtains a local date based on the specified temporal.266* A {@code TemporalAccessor} represents an arbitrary set of date and time information,267* which this factory converts to an instance of {@code ChronoLocalDate}.268* <p>269* The conversion extracts and combines the chronology and the date270* from the temporal object. The behavior is equivalent to using271* {@link Chronology#date(TemporalAccessor)} with the extracted chronology.272* Implementations are permitted to perform optimizations such as accessing273* those fields that are equivalent to the relevant objects.274* <p>275* This method matches the signature of the functional interface {@link TemporalQuery}276* allowing it to be used as a query via method reference, {@code ChronoLocalDate::from}.277*278* @param temporal the temporal object to convert, not null279* @return the date, not null280* @throws DateTimeException if unable to convert to a {@code ChronoLocalDate}281* @see Chronology#date(TemporalAccessor)282*/283static ChronoLocalDate from(TemporalAccessor temporal) {284if (temporal instanceof ChronoLocalDate) {285return (ChronoLocalDate) temporal;286}287Objects.requireNonNull(temporal, "temporal");288Chronology chrono = temporal.query(TemporalQueries.chronology());289if (chrono == null) {290throw new DateTimeException("Unable to obtain ChronoLocalDate from TemporalAccessor: " + temporal.getClass());291}292return chrono.date(temporal);293}294295//-----------------------------------------------------------------------296/**297* Gets the chronology of this date.298* <p>299* The {@code Chronology} represents the calendar system in use.300* The era and other fields in {@link ChronoField} are defined by the chronology.301*302* @return the chronology, not null303*/304Chronology getChronology();305306/**307* Gets the era, as defined by the chronology.308* <p>309* The era is, conceptually, the largest division of the time-line.310* Most calendar systems have a single epoch dividing the time-line into two eras.311* However, some have multiple eras, such as one for the reign of each leader.312* The exact meaning is determined by the {@code Chronology}.313* <p>314* All correctly implemented {@code Era} classes are singletons, thus it315* is valid code to write {@code date.getEra() == SomeChrono.ERA_NAME)}.316* <p>317* This default implementation uses {@link Chronology#eraOf(int)}.318*319* @return the chronology specific era constant applicable at this date, not null320*/321default Era getEra() {322return getChronology().eraOf(get(ERA));323}324325/**326* Checks if the year is a leap year, as defined by the calendar system.327* <p>328* A leap-year is a year of a longer length than normal.329* The exact meaning is determined by the chronology with the constraint that330* a leap-year must imply a year-length longer than a non leap-year.331* <p>332* This default implementation uses {@link Chronology#isLeapYear(long)}.333*334* @return true if this date is in a leap year, false otherwise335*/336default boolean isLeapYear() {337return getChronology().isLeapYear(getLong(YEAR));338}339340/**341* Returns the length of the month represented by this date, as defined by the calendar system.342* <p>343* This returns the length of the month in days.344*345* @return the length of the month in days346*/347int lengthOfMonth();348349/**350* Returns the length of the year represented by this date, as defined by the calendar system.351* <p>352* This returns the length of the year in days.353* <p>354* The default implementation uses {@link #isLeapYear()} and returns 365 or 366.355*356* @return the length of the year in days357*/358default int lengthOfYear() {359return (isLeapYear() ? 366 : 365);360}361362/**363* Checks if the specified field is supported.364* <p>365* This checks if the specified field can be queried on this date.366* If false, then calling the {@link #range(TemporalField) range},367* {@link #get(TemporalField) get} and {@link #with(TemporalField, long)}368* methods will throw an exception.369* <p>370* The set of supported fields is defined by the chronology and normally includes371* all {@code ChronoField} date fields.372* <p>373* If the field is not a {@code ChronoField}, then the result of this method374* is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)}375* passing {@code this} as the argument.376* Whether the field is supported is determined by the field.377*378* @param field the field to check, null returns false379* @return true if the field can be queried, false if not380*/381@Override382default boolean isSupported(TemporalField field) {383if (field instanceof ChronoField) {384return field.isDateBased();385}386return field != null && field.isSupportedBy(this);387}388389/**390* Checks if the specified unit is supported.391* <p>392* This checks if the specified unit can be added to or subtracted from this date.393* If false, then calling the {@link #plus(long, TemporalUnit)} and394* {@link #minus(long, TemporalUnit) minus} methods will throw an exception.395* <p>396* The set of supported units is defined by the chronology and normally includes397* all {@code ChronoUnit} date units except {@code FOREVER}.398* <p>399* If the unit is not a {@code ChronoUnit}, then the result of this method400* is obtained by invoking {@code TemporalUnit.isSupportedBy(Temporal)}401* passing {@code this} as the argument.402* Whether the unit is supported is determined by the unit.403*404* @param unit the unit to check, null returns false405* @return true if the unit can be added/subtracted, false if not406*/407@Override408default boolean isSupported(TemporalUnit unit) {409if (unit instanceof ChronoUnit) {410return unit.isDateBased();411}412return unit != null && unit.isSupportedBy(this);413}414415//-----------------------------------------------------------------------416// override for covariant return type417/**418* {@inheritDoc}419* @throws DateTimeException {@inheritDoc}420* @throws ArithmeticException {@inheritDoc}421*/422@Override423default ChronoLocalDate with(TemporalAdjuster adjuster) {424return ChronoLocalDateImpl.ensureValid(getChronology(), Temporal.super.with(adjuster));425}426427/**428* {@inheritDoc}429* @throws DateTimeException {@inheritDoc}430* @throws UnsupportedTemporalTypeException {@inheritDoc}431* @throws ArithmeticException {@inheritDoc}432*/433@Override434default ChronoLocalDate with(TemporalField field, long newValue) {435if (field instanceof ChronoField) {436throw new UnsupportedTemporalTypeException("Unsupported field: " + field);437}438return ChronoLocalDateImpl.ensureValid(getChronology(), field.adjustInto(this, newValue));439}440441/**442* {@inheritDoc}443* @throws DateTimeException {@inheritDoc}444* @throws ArithmeticException {@inheritDoc}445*/446@Override447default ChronoLocalDate plus(TemporalAmount amount) {448return ChronoLocalDateImpl.ensureValid(getChronology(), Temporal.super.plus(amount));449}450451/**452* {@inheritDoc}453* @throws DateTimeException {@inheritDoc}454* @throws ArithmeticException {@inheritDoc}455*/456@Override457default ChronoLocalDate plus(long amountToAdd, TemporalUnit unit) {458if (unit instanceof ChronoUnit) {459throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);460}461return ChronoLocalDateImpl.ensureValid(getChronology(), unit.addTo(this, amountToAdd));462}463464/**465* {@inheritDoc}466* @throws DateTimeException {@inheritDoc}467* @throws ArithmeticException {@inheritDoc}468*/469@Override470default ChronoLocalDate minus(TemporalAmount amount) {471return ChronoLocalDateImpl.ensureValid(getChronology(), Temporal.super.minus(amount));472}473474/**475* {@inheritDoc}476* @throws DateTimeException {@inheritDoc}477* @throws UnsupportedTemporalTypeException {@inheritDoc}478* @throws ArithmeticException {@inheritDoc}479*/480@Override481default ChronoLocalDate minus(long amountToSubtract, TemporalUnit unit) {482return ChronoLocalDateImpl.ensureValid(getChronology(), Temporal.super.minus(amountToSubtract, unit));483}484485//-----------------------------------------------------------------------486/**487* Queries this date using the specified query.488* <p>489* This queries this date using the specified query strategy object.490* The {@code TemporalQuery} object defines the logic to be used to491* obtain the result. Read the documentation of the query to understand492* what the result of this method will be.493* <p>494* The result of this method is obtained by invoking the495* {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the496* specified query passing {@code this} as the argument.497*498* @param <R> the type of the result499* @param query the query to invoke, not null500* @return the query result, null may be returned (defined by the query)501* @throws DateTimeException if unable to query (defined by the query)502* @throws ArithmeticException if numeric overflow occurs (defined by the query)503*/504@SuppressWarnings("unchecked")505@Override506default <R> R query(TemporalQuery<R> query) {507if (query == TemporalQueries.zoneId() || query == TemporalQueries.zone() || query == TemporalQueries.offset()) {508return null;509} else if (query == TemporalQueries.localTime()) {510return null;511} else if (query == TemporalQueries.chronology()) {512return (R) getChronology();513} else if (query == TemporalQueries.precision()) {514return (R) DAYS;515}516// inline TemporalAccessor.super.query(query) as an optimization517// non-JDK classes are not permitted to make this optimization518return query.queryFrom(this);519}520521/**522* Adjusts the specified temporal object to have the same date as this object.523* <p>524* This returns a temporal object of the same observable type as the input525* with the date changed to be the same as this.526* <p>527* The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}528* passing {@link ChronoField#EPOCH_DAY} as the field.529* <p>530* In most cases, it is clearer to reverse the calling pattern by using531* {@link Temporal#with(TemporalAdjuster)}:532* <pre>533* // these two lines are equivalent, but the second approach is recommended534* temporal = thisLocalDate.adjustInto(temporal);535* temporal = temporal.with(thisLocalDate);536* </pre>537* <p>538* This instance is immutable and unaffected by this method call.539*540* @param temporal the target object to be adjusted, not null541* @return the adjusted object, not null542* @throws DateTimeException if unable to make the adjustment543* @throws ArithmeticException if numeric overflow occurs544*/545@Override546default Temporal adjustInto(Temporal temporal) {547return temporal.with(EPOCH_DAY, toEpochDay());548}549550/**551* Calculates the amount of time until another date in terms of the specified unit.552* <p>553* This calculates the amount of time between two {@code ChronoLocalDate}554* objects in terms of a single {@code TemporalUnit}.555* The start and end points are {@code this} and the specified date.556* The result will be negative if the end is before the start.557* The {@code Temporal} passed to this method is converted to a558* {@code ChronoLocalDate} using {@link Chronology#date(TemporalAccessor)}.559* The calculation returns a whole number, representing the number of560* complete units between the two dates.561* For example, the amount in days between two dates can be calculated562* using {@code startDate.until(endDate, DAYS)}.563* <p>564* There are two equivalent ways of using this method.565* The first is to invoke this method.566* The second is to use {@link TemporalUnit#between(Temporal, Temporal)}:567* <pre>568* // these two lines are equivalent569* amount = start.until(end, MONTHS);570* amount = MONTHS.between(start, end);571* </pre>572* The choice should be made based on which makes the code more readable.573* <p>574* The calculation is implemented in this method for {@link ChronoUnit}.575* The units {@code DAYS}, {@code WEEKS}, {@code MONTHS}, {@code YEARS},576* {@code DECADES}, {@code CENTURIES}, {@code MILLENNIA} and {@code ERAS}577* should be supported by all implementations.578* Other {@code ChronoUnit} values will throw an exception.579* <p>580* If the unit is not a {@code ChronoUnit}, then the result of this method581* is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)}582* passing {@code this} as the first argument and the converted input temporal as583* the second argument.584* <p>585* This instance is immutable and unaffected by this method call.586*587* @param endExclusive the end date, exclusive, which is converted to a588* {@code ChronoLocalDate} in the same chronology, not null589* @param unit the unit to measure the amount in, not null590* @return the amount of time between this date and the end date591* @throws DateTimeException if the amount cannot be calculated, or the end592* temporal cannot be converted to a {@code ChronoLocalDate}593* @throws UnsupportedTemporalTypeException if the unit is not supported594* @throws ArithmeticException if numeric overflow occurs595*/596@Override // override for Javadoc597long until(Temporal endExclusive, TemporalUnit unit);598599/**600* Calculates the period between this date and another date as a {@code ChronoPeriod}.601* <p>602* This calculates the period between two dates. All supplied chronologies603* calculate the period using years, months and days, however the604* {@code ChronoPeriod} API allows the period to be represented using other units.605* <p>606* The start and end points are {@code this} and the specified date.607* The result will be negative if the end is before the start.608* The negative sign will be the same in each of year, month and day.609* <p>610* The calculation is performed using the chronology of this date.611* If necessary, the input date will be converted to match.612* <p>613* This instance is immutable and unaffected by this method call.614*615* @param endDateExclusive the end date, exclusive, which may be in any chronology, not null616* @return the period between this date and the end date, not null617* @throws DateTimeException if the period cannot be calculated618* @throws ArithmeticException if numeric overflow occurs619*/620ChronoPeriod until(ChronoLocalDate endDateExclusive);621622/**623* Formats this date using the specified formatter.624* <p>625* This date will be passed to the formatter to produce a string.626* <p>627* The default implementation must behave as follows:628* <pre>629* return formatter.format(this);630* </pre>631*632* @param formatter the formatter to use, not null633* @return the formatted date string, not null634* @throws DateTimeException if an error occurs during printing635*/636default String format(DateTimeFormatter formatter) {637Objects.requireNonNull(formatter, "formatter");638return formatter.format(this);639}640641//-----------------------------------------------------------------------642/**643* Combines this date with a time to create a {@code ChronoLocalDateTime}.644* <p>645* This returns a {@code ChronoLocalDateTime} formed from this date at the specified time.646* All possible combinations of date and time are valid.647*648* @param localTime the local time to use, not null649* @return the local date-time formed from this date and the specified time, not null650*/651@SuppressWarnings("unchecked")652default ChronoLocalDateTime<?> atTime(LocalTime localTime) {653return ChronoLocalDateTimeImpl.of(this, localTime);654}655656//-----------------------------------------------------------------------657/**658* Converts this date to the Epoch Day.659* <p>660* The {@link ChronoField#EPOCH_DAY Epoch Day count} is a simple661* incrementing count of days where day 0 is 1970-01-01 (ISO).662* This definition is the same for all chronologies, enabling conversion.663* <p>664* This default implementation queries the {@code EPOCH_DAY} field.665*666* @return the Epoch Day equivalent to this date667*/668default long toEpochDay() {669return getLong(EPOCH_DAY);670}671672//-----------------------------------------------------------------------673/**674* Compares this date to another date, including the chronology.675* <p>676* The comparison is based first on the underlying time-line date, then677* on the chronology.678* It is "consistent with equals", as defined by {@link Comparable}.679* <p>680* For example, the following is the comparator order:681* <ol>682* <li>{@code 2012-12-03 (ISO)}</li>683* <li>{@code 2012-12-04 (ISO)}</li>684* <li>{@code 2555-12-04 (ThaiBuddhist)}</li>685* <li>{@code 2012-12-05 (ISO)}</li>686* </ol>687* Values #2 and #3 represent the same date on the time-line.688* When two values represent the same date, the chronology ID is compared to distinguish them.689* This step is needed to make the ordering "consistent with equals".690* <p>691* If all the date objects being compared are in the same chronology, then the692* additional chronology stage is not required and only the local date is used.693* To compare the dates of two {@code TemporalAccessor} instances, including dates694* in two different chronologies, use {@link ChronoField#EPOCH_DAY} as a comparator.695* <p>696* This default implementation performs the comparison defined above.697*698* @param other the other date to compare to, not null699* @return the comparator value, negative if less, positive if greater700*/701@Override702default int compareTo(ChronoLocalDate other) {703int cmp = Long.compare(toEpochDay(), other.toEpochDay());704if (cmp == 0) {705cmp = getChronology().compareTo(other.getChronology());706}707return cmp;708}709710/**711* Checks if this date is after the specified date ignoring the chronology.712* <p>713* This method differs from the comparison in {@link #compareTo} in that it714* only compares the underlying date and not the chronology.715* This allows dates in different calendar systems to be compared based716* on the time-line position.717* This is equivalent to using {@code date1.toEpochDay() > date2.toEpochDay()}.718* <p>719* This default implementation performs the comparison based on the epoch-day.720*721* @param other the other date to compare to, not null722* @return true if this is after the specified date723*/724default boolean isAfter(ChronoLocalDate other) {725return this.toEpochDay() > other.toEpochDay();726}727728/**729* Checks if this date is before the specified date ignoring the chronology.730* <p>731* This method differs from the comparison in {@link #compareTo} in that it732* only compares the underlying date and not the chronology.733* This allows dates in different calendar systems to be compared based734* on the time-line position.735* This is equivalent to using {@code date1.toEpochDay() < date2.toEpochDay()}.736* <p>737* This default implementation performs the comparison based on the epoch-day.738*739* @param other the other date to compare to, not null740* @return true if this is before the specified date741*/742default boolean isBefore(ChronoLocalDate other) {743return this.toEpochDay() < other.toEpochDay();744}745746/**747* Checks if this date is equal to the specified date ignoring the chronology.748* <p>749* This method differs from the comparison in {@link #compareTo} in that it750* only compares the underlying date and not the chronology.751* This allows dates in different calendar systems to be compared based752* on the time-line position.753* This is equivalent to using {@code date1.toEpochDay() == date2.toEpochDay()}.754* <p>755* This default implementation performs the comparison based on the epoch-day.756*757* @param other the other date to compare to, not null758* @return true if the underlying date is equal to the specified date759*/760default boolean isEqual(ChronoLocalDate other) {761return this.toEpochDay() == other.toEpochDay();762}763764//-----------------------------------------------------------------------765/**766* Checks if this date is equal to another date, including the chronology.767* <p>768* Compares this date with another ensuring that the date and chronology are the same.769* <p>770* To compare the dates of two {@code TemporalAccessor} instances, including dates771* in two different chronologies, use {@link ChronoField#EPOCH_DAY} as a comparator.772*773* @param obj the object to check, null returns false774* @return true if this is equal to the other date775*/776@Override777boolean equals(Object obj);778779/**780* A hash code for this date.781*782* @return a suitable hash code783*/784@Override785int hashCode();786787//-----------------------------------------------------------------------788/**789* Outputs this date as a {@code String}.790* <p>791* The output will include the full local date.792*793* @return the formatted date, not null794*/795@Override796String toString();797798}799800801