Path: blob/master/src/java.base/share/classes/java/time/chrono/ChronoLocalDate.java
41159 views
/*1* Copyright (c) 2012, 2019, 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.io.Serializable;69import java.time.DateTimeException;70import java.time.LocalDate;71import java.time.LocalTime;72import java.time.format.DateTimeFormatter;73import java.time.temporal.ChronoField;74import java.time.temporal.ChronoUnit;75import java.time.temporal.Temporal;76import java.time.temporal.TemporalAccessor;77import java.time.temporal.TemporalAdjuster;78import java.time.temporal.TemporalAmount;79import java.time.temporal.TemporalField;80import java.time.temporal.TemporalQueries;81import java.time.temporal.TemporalQuery;82import java.time.temporal.TemporalUnit;83import java.time.temporal.UnsupportedTemporalTypeException;84import java.util.Comparator;85import java.util.Objects;8687/**88* A date without time-of-day or time-zone in an arbitrary chronology, intended89* for advanced globalization use cases.90* <p>91* <b>Most applications should declare method signatures, fields and variables92* as {@link LocalDate}, not this interface.</b>93* <p>94* A {@code ChronoLocalDate} is the abstract representation of a date where the95* {@code Chronology chronology}, or calendar system, is pluggable.96* The date is defined in terms of fields expressed by {@link TemporalField},97* where most common implementations are defined in {@link ChronoField}.98* The chronology defines how the calendar system operates and the meaning of99* the standard fields.100*101* <h2>When to use this interface</h2>102* The design of the API encourages the use of {@code LocalDate} rather than this103* interface, even in the case where the application needs to deal with multiple104* calendar systems.105* <p>106* This concept can seem surprising at first, as the natural way to globalize an107* application might initially appear to be to abstract the calendar system.108* However, as explored below, abstracting the calendar system is usually the wrong109* approach, resulting in logic errors and hard to find bugs.110* As such, it should be considered an application-wide architectural decision to choose111* to use this interface as opposed to {@code LocalDate}.112*113* <h3>Architectural issues to consider</h3>114* These are some of the points that must be considered before using this interface115* throughout an application.116* <p>117* 1) Applications using this interface, as opposed to using just {@code LocalDate},118* face a significantly higher probability of bugs. This is because the calendar system119* in use is not known at development time. A key cause of bugs is where the developer120* applies assumptions from their day-to-day knowledge of the ISO calendar system121* to code that is intended to deal with any arbitrary calendar system.122* The section below outlines how those assumptions can cause problems123* The primary mechanism for reducing this increased risk of bugs is a strong code review process.124* This should also be considered a extra cost in maintenance for the lifetime of the code.125* <p>126* 2) This interface does not enforce immutability of implementations.127* While the implementation notes indicate that all implementations must be immutable128* there is nothing in the code or type system to enforce this. Any method declared129* to accept a {@code ChronoLocalDate} could therefore be passed a poorly or130* maliciously written mutable implementation.131* <p>132* 3) Applications using this interface must consider the impact of eras.133* {@code LocalDate} shields users from the concept of eras, by ensuring that {@code getYear()}134* returns the proleptic year. That decision ensures that developers can think of135* {@code LocalDate} instances as consisting of three fields - year, month-of-year and day-of-month.136* By contrast, users of this interface must think of dates as consisting of four fields -137* era, year-of-era, month-of-year and day-of-month. The extra era field is frequently138* forgotten, yet it is of vital importance to dates in an arbitrary calendar system.139* For example, in the Japanese calendar system, the era represents the reign of an Emperor.140* Whenever one reign ends and another starts, the year-of-era is reset to one.141* <p>142* 4) The only agreed international standard for passing a date between two systems143* is the ISO-8601 standard which requires the ISO calendar system. Using this interface144* throughout the application will inevitably lead to the requirement to pass the date145* across a network or component boundary, requiring an application specific protocol or format.146* <p>147* 5) Long term persistence, such as a database, will almost always only accept dates in the148* ISO-8601 calendar system (or the related Julian-Gregorian). Passing around dates in other149* calendar systems increases the complications of interacting with persistence.150* <p>151* 6) Most of the time, passing a {@code ChronoLocalDate} throughout an application152* is unnecessary, as discussed in the last section below.153*154* <h3>False assumptions causing bugs in multi-calendar system code</h3>155* As indicated above, there are many issues to consider when try to use and manipulate a156* date in an arbitrary calendar system. These are some of the key issues.157* <p>158* Code that queries the day-of-month and assumes that the value will never be more than159* 31 is invalid. Some calendar systems have more than 31 days in some months.160* <p>161* Code that adds 12 months to a date and assumes that a year has been added is invalid.162* Some calendar systems have a different number of months, such as 13 in the Coptic or Ethiopic.163* <p>164* Code that adds one month to a date and assumes that the month-of-year value will increase165* by one or wrap to the next year is invalid. Some calendar systems have a variable number166* of months in a year, such as the Hebrew.167* <p>168* Code that adds one month, then adds a second one month and assumes that the day-of-month169* will remain close to its original value is invalid. Some calendar systems have a large difference170* between the length of the longest month and the length of the shortest month.171* For example, the Coptic or Ethiopic have 12 months of 30 days and 1 month of 5 days.172* <p>173* Code that adds seven days and assumes that a week has been added is invalid.174* Some calendar systems have weeks of other than seven days, such as the French Revolutionary.175* <p>176* Code that assumes that because the year of {@code date1} is greater than the year of {@code date2}177* then {@code date1} is after {@code date2} is invalid. This is invalid for all calendar systems178* when referring to the year-of-era, and especially untrue of the Japanese calendar system179* where the year-of-era restarts with the reign of every new Emperor.180* <p>181* Code that treats month-of-year one and day-of-month one as the start of the year is invalid.182* Not all calendar systems start the year when the month value is one.183* <p>184* In general, manipulating a date, and even querying a date, is wide open to bugs when the185* calendar system is unknown at development time. This is why it is essential that code using186* this interface is subjected to additional code reviews. It is also why an architectural187* decision to avoid this interface type is usually the correct one.188*189* <h3>Using LocalDate instead</h3>190* The primary alternative to using this interface throughout your application is as follows.191* <ul>192* <li>Declare all method signatures referring to dates in terms of {@code LocalDate}.193* <li>Either store the chronology (calendar system) in the user profile or lookup194* the chronology from the user locale195* <li>Convert the ISO {@code LocalDate} to and from the user's preferred calendar system during196* printing and parsing197* </ul>198* This approach treats the problem of globalized calendar systems as a localization issue199* and confines it to the UI layer. This approach is in keeping with other localization200* issues in the java platform.201* <p>202* As discussed above, performing calculations on a date where the rules of the calendar system203* are pluggable requires skill and is not recommended.204* Fortunately, the need to perform calculations on a date in an arbitrary calendar system205* is extremely rare. For example, it is highly unlikely that the business rules of a library206* book rental scheme will allow rentals to be for one month, where meaning of the month207* is dependent on the user's preferred calendar system.208* <p>209* A key use case for calculations on a date in an arbitrary calendar system is producing210* a month-by-month calendar for display and user interaction. Again, this is a UI issue,211* and use of this interface solely within a few methods of the UI layer may be justified.212* <p>213* In any other part of the system, where a date must be manipulated in a calendar system214* other than ISO, the use case will generally specify the calendar system to use.215* For example, an application may need to calculate the next Islamic or Hebrew holiday216* which may require manipulating the date.217* This kind of use case can be handled as follows:218* <ul>219* <li>start from the ISO {@code LocalDate} being passed to the method220* <li>convert the date to the alternate calendar system, which for this use case is known221* rather than arbitrary222* <li>perform the calculation223* <li>convert back to {@code LocalDate}224* </ul>225* Developers writing low-level frameworks or libraries should also avoid this interface.226* Instead, one of the two general purpose access interfaces should be used.227* Use {@link TemporalAccessor} if read-only access is required, or use {@link Temporal}228* if read-write access is required.229*230* @implSpec231* This interface must be implemented with care to ensure other classes operate correctly.232* All implementations that can be instantiated must be final, immutable and thread-safe.233* Subclasses should be Serializable wherever possible.234* <p>235* Additional calendar systems may be added to the system.236* See {@link Chronology} for more details.237*238* @since 1.8239*/240public interface ChronoLocalDate241extends Temporal, TemporalAdjuster, Comparable<ChronoLocalDate> {242243/**244* Gets a comparator that compares {@code ChronoLocalDate} in245* time-line order ignoring the chronology.246* <p>247* This comparator differs from the comparison in {@link #compareTo} in that it248* only compares the underlying date and not the chronology.249* This allows dates in different calendar systems to be compared based250* on the position of the date on the local time-line.251* The underlying comparison is equivalent to comparing the epoch-day.252*253* @return a comparator that compares in time-line order ignoring the chronology254* @see #isAfter255* @see #isBefore256* @see #isEqual257*/258static Comparator<ChronoLocalDate> timeLineOrder() {259return (Comparator<ChronoLocalDate> & Serializable) (date1, date2) -> {260return Long.compare(date1.toEpochDay(), date2.toEpochDay());261};262}263264//-----------------------------------------------------------------------265/**266* Obtains an instance of {@code ChronoLocalDate} from a temporal object.267* <p>268* This obtains a local date based on the specified temporal.269* A {@code TemporalAccessor} represents an arbitrary set of date and time information,270* which this factory converts to an instance of {@code ChronoLocalDate}.271* <p>272* The conversion extracts and combines the chronology and the date273* from the temporal object. The behavior is equivalent to using274* {@link Chronology#date(TemporalAccessor)} with the extracted chronology.275* Implementations are permitted to perform optimizations such as accessing276* those fields that are equivalent to the relevant objects.277* <p>278* This method matches the signature of the functional interface {@link TemporalQuery}279* allowing it to be used as a query via method reference, {@code ChronoLocalDate::from}.280*281* @param temporal the temporal object to convert, not null282* @return the date, not null283* @throws DateTimeException if unable to convert to a {@code ChronoLocalDate}284* @see Chronology#date(TemporalAccessor)285*/286static ChronoLocalDate from(TemporalAccessor temporal) {287if (temporal instanceof ChronoLocalDate) {288return (ChronoLocalDate) temporal;289}290Objects.requireNonNull(temporal, "temporal");291Chronology chrono = temporal.query(TemporalQueries.chronology());292if (chrono == null) {293throw new DateTimeException("Unable to obtain ChronoLocalDate from TemporalAccessor: " + temporal.getClass());294}295return chrono.date(temporal);296}297298//-----------------------------------------------------------------------299/**300* Gets the chronology of this date.301* <p>302* The {@code Chronology} represents the calendar system in use.303* The era and other fields in {@link ChronoField} are defined by the chronology.304*305* @return the chronology, not null306*/307Chronology getChronology();308309/**310* Gets the era, as defined by the chronology.311* <p>312* The era is, conceptually, the largest division of the time-line.313* Most calendar systems have a single epoch dividing the time-line into two eras.314* However, some have multiple eras, such as one for the reign of each leader.315* The exact meaning is determined by the {@code Chronology}.316* <p>317* All correctly implemented {@code Era} classes are singletons, thus it318* is valid code to write {@code date.getEra() == SomeChrono.ERA_NAME)}.319* <p>320* This default implementation uses {@link Chronology#eraOf(int)}.321*322* @return the chronology specific era constant applicable at this date, not null323*/324default Era getEra() {325return getChronology().eraOf(get(ERA));326}327328/**329* Checks if the year is a leap year, as defined by the calendar system.330* <p>331* A leap-year is a year of a longer length than normal.332* The exact meaning is determined by the chronology with the constraint that333* a leap-year must imply a year-length longer than a non leap-year.334* <p>335* This default implementation uses {@link Chronology#isLeapYear(long)}.336*337* @return true if this date is in a leap year, false otherwise338*/339default boolean isLeapYear() {340return getChronology().isLeapYear(getLong(YEAR));341}342343/**344* Returns the length of the month represented by this date, as defined by the calendar system.345* <p>346* This returns the length of the month in days.347*348* @return the length of the month in days349*/350int lengthOfMonth();351352/**353* Returns the length of the year represented by this date, as defined by the calendar system.354* <p>355* This returns the length of the year in days.356* <p>357* The default implementation uses {@link #isLeapYear()} and returns 365 or 366.358*359* @return the length of the year in days360*/361default int lengthOfYear() {362return (isLeapYear() ? 366 : 365);363}364365/**366* Checks if the specified field is supported.367* <p>368* This checks if the specified field can be queried on this date.369* If false, then calling the {@link #range(TemporalField) range},370* {@link #get(TemporalField) get} and {@link #with(TemporalField, long)}371* methods will throw an exception.372* <p>373* The set of supported fields is defined by the chronology and normally includes374* all {@code ChronoField} date fields.375* <p>376* If the field is not a {@code ChronoField}, then the result of this method377* is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)}378* passing {@code this} as the argument.379* Whether the field is supported is determined by the field.380*381* @param field the field to check, null returns false382* @return true if the field can be queried, false if not383*/384@Override385default boolean isSupported(TemporalField field) {386if (field instanceof ChronoField) {387return field.isDateBased();388}389return field != null && field.isSupportedBy(this);390}391392/**393* Checks if the specified unit is supported.394* <p>395* This checks if the specified unit can be added to or subtracted from this date.396* If false, then calling the {@link #plus(long, TemporalUnit)} and397* {@link #minus(long, TemporalUnit) minus} methods will throw an exception.398* <p>399* The set of supported units is defined by the chronology and normally includes400* all {@code ChronoUnit} date units except {@code FOREVER}.401* <p>402* If the unit is not a {@code ChronoUnit}, then the result of this method403* is obtained by invoking {@code TemporalUnit.isSupportedBy(Temporal)}404* passing {@code this} as the argument.405* Whether the unit is supported is determined by the unit.406*407* @param unit the unit to check, null returns false408* @return true if the unit can be added/subtracted, false if not409*/410@Override411default boolean isSupported(TemporalUnit unit) {412if (unit instanceof ChronoUnit) {413return unit.isDateBased();414}415return unit != null && unit.isSupportedBy(this);416}417418//-----------------------------------------------------------------------419// override for covariant return type420/**421* {@inheritDoc}422* @throws DateTimeException {@inheritDoc}423* @throws ArithmeticException {@inheritDoc}424*/425@Override426default ChronoLocalDate with(TemporalAdjuster adjuster) {427return ChronoLocalDateImpl.ensureValid(getChronology(), Temporal.super.with(adjuster));428}429430/**431* {@inheritDoc}432* @throws DateTimeException {@inheritDoc}433* @throws UnsupportedTemporalTypeException {@inheritDoc}434* @throws ArithmeticException {@inheritDoc}435*/436@Override437default ChronoLocalDate with(TemporalField field, long newValue) {438if (field instanceof ChronoField) {439throw new UnsupportedTemporalTypeException("Unsupported field: " + field);440}441return ChronoLocalDateImpl.ensureValid(getChronology(), field.adjustInto(this, newValue));442}443444/**445* {@inheritDoc}446* @throws DateTimeException {@inheritDoc}447* @throws ArithmeticException {@inheritDoc}448*/449@Override450default ChronoLocalDate plus(TemporalAmount amount) {451return ChronoLocalDateImpl.ensureValid(getChronology(), Temporal.super.plus(amount));452}453454/**455* {@inheritDoc}456* @throws DateTimeException {@inheritDoc}457* @throws ArithmeticException {@inheritDoc}458*/459@Override460default ChronoLocalDate plus(long amountToAdd, TemporalUnit unit) {461if (unit instanceof ChronoUnit) {462throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);463}464return ChronoLocalDateImpl.ensureValid(getChronology(), unit.addTo(this, amountToAdd));465}466467/**468* {@inheritDoc}469* @throws DateTimeException {@inheritDoc}470* @throws ArithmeticException {@inheritDoc}471*/472@Override473default ChronoLocalDate minus(TemporalAmount amount) {474return ChronoLocalDateImpl.ensureValid(getChronology(), Temporal.super.minus(amount));475}476477/**478* {@inheritDoc}479* @throws DateTimeException {@inheritDoc}480* @throws UnsupportedTemporalTypeException {@inheritDoc}481* @throws ArithmeticException {@inheritDoc}482*/483@Override484default ChronoLocalDate minus(long amountToSubtract, TemporalUnit unit) {485return ChronoLocalDateImpl.ensureValid(getChronology(), Temporal.super.minus(amountToSubtract, unit));486}487488//-----------------------------------------------------------------------489/**490* Queries this date using the specified query.491* <p>492* This queries this date using the specified query strategy object.493* The {@code TemporalQuery} object defines the logic to be used to494* obtain the result. Read the documentation of the query to understand495* what the result of this method will be.496* <p>497* The result of this method is obtained by invoking the498* {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the499* specified query passing {@code this} as the argument.500*501* @param <R> the type of the result502* @param query the query to invoke, not null503* @return the query result, null may be returned (defined by the query)504* @throws DateTimeException if unable to query (defined by the query)505* @throws ArithmeticException if numeric overflow occurs (defined by the query)506*/507@SuppressWarnings("unchecked")508@Override509default <R> R query(TemporalQuery<R> query) {510if (query == TemporalQueries.zoneId() || query == TemporalQueries.zone() || query == TemporalQueries.offset()) {511return null;512} else if (query == TemporalQueries.localTime()) {513return null;514} else if (query == TemporalQueries.chronology()) {515return (R) getChronology();516} else if (query == TemporalQueries.precision()) {517return (R) DAYS;518}519// inline TemporalAccessor.super.query(query) as an optimization520// non-JDK classes are not permitted to make this optimization521return query.queryFrom(this);522}523524/**525* Adjusts the specified temporal object to have the same date as this object.526* <p>527* This returns a temporal object of the same observable type as the input528* with the date changed to be the same as this.529* <p>530* The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}531* passing {@link ChronoField#EPOCH_DAY} as the field.532* <p>533* In most cases, it is clearer to reverse the calling pattern by using534* {@link Temporal#with(TemporalAdjuster)}:535* <pre>536* // these two lines are equivalent, but the second approach is recommended537* temporal = thisLocalDate.adjustInto(temporal);538* temporal = temporal.with(thisLocalDate);539* </pre>540* <p>541* This instance is immutable and unaffected by this method call.542*543* @param temporal the target object to be adjusted, not null544* @return the adjusted object, not null545* @throws DateTimeException if unable to make the adjustment546* @throws ArithmeticException if numeric overflow occurs547*/548@Override549default Temporal adjustInto(Temporal temporal) {550return temporal.with(EPOCH_DAY, toEpochDay());551}552553/**554* Calculates the amount of time until another date in terms of the specified unit.555* <p>556* This calculates the amount of time between two {@code ChronoLocalDate}557* objects in terms of a single {@code TemporalUnit}.558* The start and end points are {@code this} and the specified date.559* The result will be negative if the end is before the start.560* The {@code Temporal} passed to this method is converted to a561* {@code ChronoLocalDate} using {@link Chronology#date(TemporalAccessor)}.562* The calculation returns a whole number, representing the number of563* complete units between the two dates.564* For example, the amount in days between two dates can be calculated565* using {@code startDate.until(endDate, DAYS)}.566* <p>567* There are two equivalent ways of using this method.568* The first is to invoke this method.569* The second is to use {@link TemporalUnit#between(Temporal, Temporal)}:570* <pre>571* // these two lines are equivalent572* amount = start.until(end, MONTHS);573* amount = MONTHS.between(start, end);574* </pre>575* The choice should be made based on which makes the code more readable.576* <p>577* The calculation is implemented in this method for {@link ChronoUnit}.578* The units {@code DAYS}, {@code WEEKS}, {@code MONTHS}, {@code YEARS},579* {@code DECADES}, {@code CENTURIES}, {@code MILLENNIA} and {@code ERAS}580* should be supported by all implementations.581* Other {@code ChronoUnit} values will throw an exception.582* <p>583* If the unit is not a {@code ChronoUnit}, then the result of this method584* is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)}585* passing {@code this} as the first argument and the converted input temporal as586* the second argument.587* <p>588* This instance is immutable and unaffected by this method call.589*590* @param endExclusive the end date, exclusive, which is converted to a591* {@code ChronoLocalDate} in the same chronology, not null592* @param unit the unit to measure the amount in, not null593* @return the amount of time between this date and the end date594* @throws DateTimeException if the amount cannot be calculated, or the end595* temporal cannot be converted to a {@code ChronoLocalDate}596* @throws UnsupportedTemporalTypeException if the unit is not supported597* @throws ArithmeticException if numeric overflow occurs598*/599@Override // override for Javadoc600long until(Temporal endExclusive, TemporalUnit unit);601602/**603* Calculates the period between this date and another date as a {@code ChronoPeriod}.604* <p>605* This calculates the period between two dates. All supplied chronologies606* calculate the period using years, months and days, however the607* {@code ChronoPeriod} API allows the period to be represented using other units.608* <p>609* The start and end points are {@code this} and the specified date.610* The result will be negative if the end is before the start.611* The negative sign will be the same in each of year, month and day.612* <p>613* The calculation is performed using the chronology of this date.614* If necessary, the input date will be converted to match.615* <p>616* This instance is immutable and unaffected by this method call.617*618* @param endDateExclusive the end date, exclusive, which may be in any chronology, not null619* @return the period between this date and the end date, not null620* @throws DateTimeException if the period cannot be calculated621* @throws ArithmeticException if numeric overflow occurs622*/623ChronoPeriod until(ChronoLocalDate endDateExclusive);624625/**626* Formats this date using the specified formatter.627* <p>628* This date will be passed to the formatter to produce a string.629* <p>630* The default implementation must behave as follows:631* <pre>632* return formatter.format(this);633* </pre>634*635* @param formatter the formatter to use, not null636* @return the formatted date string, not null637* @throws DateTimeException if an error occurs during printing638*/639default String format(DateTimeFormatter formatter) {640Objects.requireNonNull(formatter, "formatter");641return formatter.format(this);642}643644//-----------------------------------------------------------------------645/**646* Combines this date with a time to create a {@code ChronoLocalDateTime}.647* <p>648* This returns a {@code ChronoLocalDateTime} formed from this date at the specified time.649* All possible combinations of date and time are valid.650*651* @param localTime the local time to use, not null652* @return the local date-time formed from this date and the specified time, not null653*/654@SuppressWarnings("unchecked")655default ChronoLocalDateTime<?> atTime(LocalTime localTime) {656return ChronoLocalDateTimeImpl.of(this, localTime);657}658659//-----------------------------------------------------------------------660/**661* Converts this date to the Epoch Day.662* <p>663* The {@link ChronoField#EPOCH_DAY Epoch Day count} is a simple664* incrementing count of days where day 0 is 1970-01-01 (ISO).665* This definition is the same for all chronologies, enabling conversion.666* <p>667* This default implementation queries the {@code EPOCH_DAY} field.668*669* @return the Epoch Day equivalent to this date670*/671default long toEpochDay() {672return getLong(EPOCH_DAY);673}674675//-----------------------------------------------------------------------676/**677* Compares this date to another date, including the chronology.678* <p>679* The comparison is based first on the underlying time-line date, then680* on the chronology.681* It is "consistent with equals", as defined by {@link Comparable}.682* <p>683* For example, the following is the comparator order:684* <ol>685* <li>{@code 2012-12-03 (ISO)}</li>686* <li>{@code 2012-12-04 (ISO)}</li>687* <li>{@code 2555-12-04 (ThaiBuddhist)}</li>688* <li>{@code 2012-12-05 (ISO)}</li>689* </ol>690* Values #2 and #3 represent the same date on the time-line.691* When two values represent the same date, the chronology ID is compared to distinguish them.692* This step is needed to make the ordering "consistent with equals".693* <p>694* If all the date objects being compared are in the same chronology, then the695* additional chronology stage is not required and only the local date is used.696* To compare the dates of two {@code TemporalAccessor} instances, including dates697* in two different chronologies, use {@link ChronoField#EPOCH_DAY} as a comparator.698* <p>699* This default implementation performs the comparison defined above.700*701* @param other the other date to compare to, not null702* @return the comparator value, negative if less, positive if greater703*/704@Override705default int compareTo(ChronoLocalDate other) {706int cmp = Long.compare(toEpochDay(), other.toEpochDay());707if (cmp == 0) {708cmp = getChronology().compareTo(other.getChronology());709}710return cmp;711}712713/**714* Checks if this date is after the specified date ignoring the chronology.715* <p>716* This method differs from the comparison in {@link #compareTo} in that it717* only compares the underlying date and not the chronology.718* This allows dates in different calendar systems to be compared based719* on the time-line position.720* This is equivalent to using {@code date1.toEpochDay() > date2.toEpochDay()}.721* <p>722* This default implementation performs the comparison based on the epoch-day.723*724* @param other the other date to compare to, not null725* @return true if this is after the specified date726*/727default boolean isAfter(ChronoLocalDate other) {728return this.toEpochDay() > other.toEpochDay();729}730731/**732* Checks if this date is before the specified date ignoring the chronology.733* <p>734* This method differs from the comparison in {@link #compareTo} in that it735* only compares the underlying date and not the chronology.736* This allows dates in different calendar systems to be compared based737* on the time-line position.738* This is equivalent to using {@code date1.toEpochDay() < date2.toEpochDay()}.739* <p>740* This default implementation performs the comparison based on the epoch-day.741*742* @param other the other date to compare to, not null743* @return true if this is before the specified date744*/745default boolean isBefore(ChronoLocalDate other) {746return this.toEpochDay() < other.toEpochDay();747}748749/**750* Checks if this date is equal to the specified date ignoring the chronology.751* <p>752* This method differs from the comparison in {@link #compareTo} in that it753* only compares the underlying date and not the chronology.754* This allows dates in different calendar systems to be compared based755* on the time-line position.756* This is equivalent to using {@code date1.toEpochDay() == date2.toEpochDay()}.757* <p>758* This default implementation performs the comparison based on the epoch-day.759*760* @param other the other date to compare to, not null761* @return true if the underlying date is equal to the specified date762*/763default boolean isEqual(ChronoLocalDate other) {764return this.toEpochDay() == other.toEpochDay();765}766767//-----------------------------------------------------------------------768/**769* Checks if this date is equal to another date, including the chronology.770* <p>771* Compares this date with another ensuring that the date and chronology are the same.772* <p>773* To compare the dates of two {@code TemporalAccessor} instances, including dates774* in two different chronologies, use {@link ChronoField#EPOCH_DAY} as a comparator.775*776* @param obj the object to check, null returns false777* @return true if this is equal to the other date778*/779@Override780boolean equals(Object obj);781782/**783* A hash code for this date.784*785* @return a suitable hash code786*/787@Override788int hashCode();789790//-----------------------------------------------------------------------791/**792* Outputs this date as a {@code String}.793* <p>794* The output will include the full local date.795*796* @return the formatted date, not null797*/798@Override799String toString();800801}802803804