1 /* 2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 /* 27 * This file is available under and governed by the GNU General Public 28 * License version 2 only, as published by the Free Software Foundation. 29 * However, the following notice accompanied the original version of this 30 * file: 31 * 32 * Copyright (c) 2012, Stephen Colebourne & Michael Nascimento Santos 33 * 34 * All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions are met: 38 * 39 * * Redistributions of source code must retain the above copyright notice, 40 * this list of conditions and the following disclaimer. 41 * 42 * * Redistributions in binary form must reproduce the above copyright notice, 43 * this list of conditions and the following disclaimer in the documentation 44 * and/or other materials provided with the distribution. 45 * 46 * * Neither the name of JSR-310 nor the names of its contributors 47 * may be used to endorse or promote products derived from this software 48 * without specific prior written permission. 49 * 50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 54 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 55 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 56 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 57 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 58 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 59 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 60 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 61 */ 62 package java.time.chrono; 63 64 import static java.time.temporal.ChronoField.EPOCH_DAY; 65 import static java.time.temporal.ChronoField.ERA; 66 import static java.time.temporal.ChronoField.YEAR; 67 import static java.time.temporal.ChronoUnit.DAYS; 68 69 import java.time.DateTimeException; 70 import java.time.LocalDate; 71 import java.time.LocalTime; 72 import java.time.Period; 73 import java.time.format.DateTimeFormatter; 74 import java.time.temporal.ChronoField; 75 import java.time.temporal.ChronoUnit; 76 import java.time.temporal.Temporal; 77 import java.time.temporal.TemporalAccessor; 78 import java.time.temporal.TemporalAdjuster; 79 import java.time.temporal.TemporalAmount; 80 import java.time.temporal.TemporalField; 81 import java.time.temporal.TemporalQuery; 82 import java.time.temporal.TemporalUnit; 83 import java.time.temporal.UnsupportedTemporalTypeException; 84 import java.util.Comparator; 85 import java.util.Objects; 86 87 /** 88 * A date without time-of-day or time-zone in an arbitrary chronology, intended 89 * for advanced globalization use cases. 90 * <p> 91 * <b>Most applications should declare method signatures, fields and variables 92 * as {@link LocalDate}, not this interface.</b> 93 * <p> 94 * A {@code ChronoLocalDate} is the abstract representation of a date where the 95 * {@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 of 99 * the standard fields. 100 * 101 * <h3>When to use this interface</h3> 102 * The design of the API encourages the use of {@code LocalDate} rather than this 103 * interface, even in the case where the application needs to deal with multiple 104 * calendar systems. The rationale for this is explored in the following documentation. 105 * <p> 106 * The primary use case where this interface should be used is where the generic 107 * type parameter {@code <D>} is fully defined as a specific chronology. 108 * In that case, the assumptions of that chronology are known at development 109 * time and specified in the code. 110 * <p> 111 * When the chronology is defined in the generic type parameter as ? or otherwise 112 * unknown at development time, the rest of the discussion below applies. 113 * <p> 114 * To emphasize the point, declaring a method signature, field or variable as this 115 * interface type can initially seem like the sensible way to globalize an application, 116 * however it is usually the wrong approach. 117 * As such, it should be considered an application-wide architectural decision to choose 118 * to use this interface as opposed to {@code LocalDate}. 119 * 120 * <h3>Architectural issues to consider</h3> 121 * These are some of the points that must be considered before using this interface 122 * throughout an application. 123 * <p> 124 * 1) Applications using this interface, as opposed to using just {@code LocalDate}, 125 * face a significantly higher probability of bugs. This is because the calendar system 126 * in use is not known at development time. A key cause of bugs is where the developer 127 * applies assumptions from their day-to-day knowledge of the ISO calendar system 128 * to code that is intended to deal with any arbitrary calendar system. 129 * The section below outlines how those assumptions can cause problems 130 * The primary mechanism for reducing this increased risk of bugs is a strong code review process. 131 * This should also be considered a extra cost in maintenance for the lifetime of the code. 132 * <p> 133 * 2) This interface does not enforce immutability of implementations. 134 * While the implementation notes indicate that all implementations must be immutable 135 * there is nothing in the code or type system to enforce this. Any method declared 136 * to accept a {@code ChronoLocalDate} could therefore be passed a poorly or 137 * maliciously written mutable implementation. 138 * <p> 139 * 3) Applications using this interface must consider the impact of eras. 140 * {@code LocalDate} shields users from the concept of eras, by ensuring that {@code getYear()} 141 * returns the proleptic year. That decision ensures that developers can think of 142 * {@code LocalDate} instances as consisting of three fields - year, month-of-year and day-of-month. 143 * By contrast, users of this interface must think of dates as consisting of four fields - 144 * era, year-of-era, month-of-year and day-of-month. The extra era field is frequently 145 * forgotten, yet it is of vital importance to dates in an arbitrary calendar system. 146 * For example, in the Japanese calendar system, the era represents the reign of an Emperor. 147 * Whenever one reign ends and another starts, the year-of-era is reset to one. 148 * <p> 149 * 4) The only agreed international standard for passing a date between two systems 150 * is the ISO-8601 standard which requires the ISO calendar system. Using this interface 151 * throughout the application will inevitably lead to the requirement to pass the date 152 * across a network or component boundary, requiring an application specific protocol or format. 153 * <p> 154 * 5) Long term persistence, such as a database, will almost always only accept dates in the 155 * ISO-8601 calendar system (or the related Julian-Gregorian). Passing around dates in other 156 * calendar systems increases the complications of interacting with persistence. 157 * <p> 158 * 6) Most of the time, passing a {@code ChronoLocalDate} throughout an application 159 * is unnecessary, as discussed in the last section below. 160 * 161 * <h3>False assumptions causing bugs in multi-calendar system code</h3> 162 * As indicated above, there are many issues to consider when try to use and manipulate a 163 * date in an arbitrary calendar system. These are some of the key issues. 164 * <p> 165 * Code that queries the day-of-month and assumes that the value will never be more than 166 * 31 is invalid. Some calendar systems have more than 31 days in some months. 167 * <p> 168 * Code that adds 12 months to a date and assumes that a year has been added is invalid. 169 * Some calendar systems have a different number of months, such as 13 in the Coptic or Ethiopic. 170 * <p> 171 * Code that adds one month to a date and assumes that the month-of-year value will increase 172 * by one or wrap to the next year is invalid. Some calendar systems have a variable number 173 * of months in a year, such as the Hebrew. 174 * <p> 175 * Code that adds one month, then adds a second one month and assumes that the day-of-month 176 * will remain close to its original value is invalid. Some calendar systems have a large difference 177 * between the length of the longest month and the length of the shortest month. 178 * For example, the Coptic or Ethiopic have 12 months of 30 days and 1 month of 5 days. 179 * <p> 180 * Code that adds seven days and assumes that a week has been added is invalid. 181 * Some calendar systems have weeks of other than seven days, such as the French Revolutionary. 182 * <p> 183 * Code that assumes that because the year of {@code date1} is greater than the year of {@code date2} 184 * then {@code date1} is after {@code date2} is invalid. This is invalid for all calendar systems 185 * when referring to the year-of-era, and especially untrue of the Japanese calendar system 186 * where the year-of-era restarts with the reign of every new Emperor. 187 * <p> 188 * Code that treats month-of-year one and day-of-month one as the start of the year is invalid. 189 * Not all calendar systems start the year when the month value is one. 190 * <p> 191 * In general, manipulating a date, and even querying a date, is wide open to bugs when the 192 * calendar system is unknown at development time. This is why it is essential that code using 193 * this interface is subjected to additional code reviews. It is also why an architectural 194 * decision to avoid this interface type is usually the correct one. 195 * 196 * <h3>Using LocalDate instead</h3> 197 * The primary alternative to using this interface throughout your application is as follows. 198 * <p><ul> 199 * <li>Declare all method signatures referring to dates in terms of {@code LocalDate}. 200 * <li>Either store the chronology (calendar system) in the user profile or lookup 201 * the chronology from the user locale 202 * <li>Convert the ISO {@code LocalDate} to and from the user's preferred calendar system during 203 * printing and parsing 204 * </ul><p> 205 * This approach treats the problem of globalized calendar systems as a localization issue 206 * and confines it to the UI layer. This approach is in keeping with other localization 207 * issues in the java platform. 208 * <p> 209 * As discussed above, performing calculations on a date where the rules of the calendar system 210 * are pluggable requires skill and is not recommended. 211 * Fortunately, the need to perform calculations on a date in an arbitrary calendar system 212 * is extremely rare. For example, it is highly unlikely that the business rules of a library 213 * book rental scheme will allow rentals to be for one month, where meaning of the month 214 * is dependent on the user's preferred calendar system. 215 * <p> 216 * A key use case for calculations on a date in an arbitrary calendar system is producing 217 * a month-by-month calendar for display and user interaction. Again, this is a UI issue, 218 * and use of this interface solely within a few methods of the UI layer may be justified. 219 * <p> 220 * In any other part of the system, where a date must be manipulated in a calendar system 221 * other than ISO, the use case will generally specify the calendar system to use. 222 * For example, an application may need to calculate the next Islamic or Hebrew holiday 223 * which may require manipulating the date. 224 * This kind of use case can be handled as follows: 225 * <p><ul> 226 * <li>start from the ISO {@code LocalDate} being passed to the method 227 * <li>convert the date to the alternate calendar system, which for this use case is known 228 * rather than arbitrary 229 * <li>perform the calculation 230 * <li>convert back to {@code LocalDate} 231 * </ul><p> 232 * Developers writing low-level frameworks or libraries should also avoid this interface. 233 * Instead, one of the two general purpose access interfaces should be used. 234 * Use {@link TemporalAccessor} if read-only access is required, or use {@link Temporal} 235 * if read-write access is required. 236 * 237 * @implSpec 238 * This interface must be implemented with care to ensure other classes operate correctly. 239 * All implementations that can be instantiated must be final, immutable and thread-safe. 240 * Subclasses should be Serializable wherever possible. 241 * <p> 242 * Additional calendar systems may be added to the system. 243 * See {@link Chronology} for more details. 244 * 245 * @param <D> the concrete type for the date 246 * @since 1.8 247 */ 248 public interface ChronoLocalDate<D extends ChronoLocalDate<D>> 249 extends Temporal, TemporalAdjuster, Comparable<ChronoLocalDate<?>> { 250 251 /** 252 * Gets a comparator that compares {@code ChronoLocalDate} in 253 * time-line order ignoring the chronology. 254 * <p> 255 * This comparator differs from the comparison in {@link #compareTo} in that it 256 * only compares the underlying date and not the chronology. 257 * This allows dates in different calendar systems to be compared based 258 * on the position of the date on the local time-line. 259 * The underlying comparison is equivalent to comparing the epoch-day. 260 * @return a comparator that compares in time-line order ignoring the chronology 261 * 262 * @see #isAfter 263 * @see #isBefore 264 * @see #isEqual 265 */ 266 static Comparator<ChronoLocalDate<?>> timeLineOrder() { 267 return Chronology.DATE_ORDER; 268 } 269 270 //----------------------------------------------------------------------- 271 /** 272 * Obtains an instance of {@code ChronoLocalDate} from a temporal object. 273 * <p> 274 * This obtains a local date based on the specified temporal. 275 * A {@code TemporalAccessor} represents an arbitrary set of date and time information, 276 * which this factory converts to an instance of {@code ChronoLocalDate}. 277 * <p> 278 * The conversion extracts and combines the chronology and the date 279 * from the temporal object. The behavior is equivalent to using 280 * {@link Chronology#date(TemporalAccessor)} with the extracted chronology. 281 * Implementations are permitted to perform optimizations such as accessing 282 * those fields that are equivalent to the relevant objects. 283 * <p> 284 * This method matches the signature of the functional interface {@link TemporalQuery} 285 * allowing it to be used as a query via method reference, {@code ChronoLocalDate::from}. 286 * 287 * @param temporal the temporal object to convert, not null 288 * @return the date, not null 289 * @throws DateTimeException if unable to convert to a {@code ChronoLocalDate} 290 * @see Chronology#date(TemporalAccessor) 291 */ 292 static ChronoLocalDate<?> from(TemporalAccessor temporal) { 293 if (temporal instanceof ChronoLocalDate) { 294 return (ChronoLocalDate<?>) temporal; 295 } 296 Chronology chrono = temporal.query(TemporalQuery.chronology()); 297 if (chrono == null) { 298 throw new DateTimeException("Unable to obtain ChronoLocalDate from TemporalAccessor: " + temporal.getClass()); 299 } 300 return chrono.date(temporal); 301 } 302 303 //----------------------------------------------------------------------- 304 /** 305 * Gets the chronology of this date. 306 * <p> 307 * The {@code Chronology} represents the calendar system in use. 308 * The era and other fields in {@link ChronoField} are defined by the chronology. 309 * 310 * @return the chronology, not null 311 */ 312 Chronology getChronology(); 313 314 /** 315 * Gets the era, as defined by the chronology. 316 * <p> 317 * The era is, conceptually, the largest division of the time-line. 318 * Most calendar systems have a single epoch dividing the time-line into two eras. 319 * However, some have multiple eras, such as one for the reign of each leader. 320 * The exact meaning is determined by the {@code Chronology}. 321 * <p> 322 * All correctly implemented {@code Era} classes are singletons, thus it 323 * is valid code to write {@code date.getEra() == SomeChrono.ERA_NAME)}. 324 * <p> 325 * This default implementation uses {@link Chronology#eraOf(int)}. 326 * 327 * @return the chronology specific era constant applicable at this date, not null 328 */ 329 default Era getEra() { 330 return getChronology().eraOf(get(ERA)); 331 } 332 333 /** 334 * Checks if the year is a leap year, as defined by the calendar system. 335 * <p> 336 * A leap-year is a year of a longer length than normal. 337 * The exact meaning is determined by the chronology with the constraint that 338 * a leap-year must imply a year-length longer than a non leap-year. 339 * <p> 340 * This default implementation uses {@link Chronology#isLeapYear(long)}. 341 * 342 * @return true if this date is in a leap year, false otherwise 343 */ 344 default boolean isLeapYear() { 345 return getChronology().isLeapYear(getLong(YEAR)); 346 } 347 348 /** 349 * Returns the length of the month represented by this date, as defined by the calendar system. 350 * <p> 351 * This returns the length of the month in days. 352 * 353 * @return the length of the month in days 354 */ 355 int lengthOfMonth(); 356 357 /** 358 * Returns the length of the year represented by this date, as defined by the calendar system. 359 * <p> 360 * This returns the length of the year in days. 361 * <p> 362 * The default implementation uses {@link #isLeapYear()} and returns 365 or 366. 363 * 364 * @return the length of the year in days 365 */ 366 default int lengthOfYear() { 367 return (isLeapYear() ? 366 : 365); 368 } 369 370 @Override 371 default boolean isSupported(TemporalField field) { 372 if (field instanceof ChronoField) { 373 return field.isDateBased(); 374 } 375 return field != null && field.isSupportedBy(this); 376 } 377 378 //----------------------------------------------------------------------- 379 // override for covariant return type 380 /** 381 * {@inheritDoc} 382 * @throws DateTimeException {@inheritDoc} 383 * @throws ArithmeticException {@inheritDoc} 384 */ 385 @Override 386 default D with(TemporalAdjuster adjuster) { 387 return (D) getChronology().ensureChronoLocalDate(Temporal.super.with(adjuster)); 388 } 389 390 /** 391 * {@inheritDoc} 392 * @throws DateTimeException {@inheritDoc} 393 * @throws UnsupportedTemporalTypeException {@inheritDoc} 394 * @throws ArithmeticException {@inheritDoc} 395 */ 396 @Override 397 default D with(TemporalField field, long newValue) { 398 if (field instanceof ChronoField) { 399 throw new UnsupportedTemporalTypeException("Unsupported field: " + field.getName()); 400 } 401 return (D) getChronology().ensureChronoLocalDate(field.adjustInto(this, newValue)); 402 } 403 404 /** 405 * {@inheritDoc} 406 * @throws DateTimeException {@inheritDoc} 407 * @throws ArithmeticException {@inheritDoc} 408 */ 409 @Override 410 default D plus(TemporalAmount amount) { 411 return (D) getChronology().ensureChronoLocalDate(Temporal.super.plus(amount)); 412 } 413 414 /** 415 * {@inheritDoc} 416 * @throws DateTimeException {@inheritDoc} 417 * @throws ArithmeticException {@inheritDoc} 418 */ 419 @Override 420 default D plus(long amountToAdd, TemporalUnit unit) { 421 if (unit instanceof ChronoUnit) { 422 throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit.getName()); 423 } 424 return (D) getChronology().ensureChronoLocalDate(unit.addTo(this, amountToAdd)); 425 } 426 427 /** 428 * {@inheritDoc} 429 * @throws DateTimeException {@inheritDoc} 430 * @throws ArithmeticException {@inheritDoc} 431 */ 432 @Override 433 default D minus(TemporalAmount amount) { 434 return (D) getChronology().ensureChronoLocalDate(Temporal.super.minus(amount)); 435 } 436 437 /** 438 * {@inheritDoc} 439 * @throws DateTimeException {@inheritDoc} 440 * @throws UnsupportedTemporalTypeException {@inheritDoc} 441 * @throws ArithmeticException {@inheritDoc} 442 */ 443 @Override 444 default D minus(long amountToSubtract, TemporalUnit unit) { 445 return (D) getChronology().ensureChronoLocalDate(Temporal.super.minus(amountToSubtract, unit)); 446 } 447 448 //----------------------------------------------------------------------- 449 /** 450 * Queries this date using the specified query. 451 * <p> 452 * This queries this date using the specified query strategy object. 453 * The {@code TemporalQuery} object defines the logic to be used to 454 * obtain the result. Read the documentation of the query to understand 455 * what the result of this method will be. 456 * <p> 457 * The result of this method is obtained by invoking the 458 * {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the 459 * specified query passing {@code this} as the argument. 460 * 461 * @param <R> the type of the result 462 * @param query the query to invoke, not null 463 * @return the query result, null may be returned (defined by the query) 464 * @throws DateTimeException if unable to query (defined by the query) 465 * @throws ArithmeticException if numeric overflow occurs (defined by the query) 466 */ 467 @SuppressWarnings("unchecked") 468 @Override 469 default <R> R query(TemporalQuery<R> query) { 470 if (query == TemporalQuery.zoneId() || query == TemporalQuery.zone() || query == TemporalQuery.offset()) { 471 return null; 472 } else if (query == TemporalQuery.localTime()) { 473 return null; 474 } else if (query == TemporalQuery.chronology()) { 475 return (R) getChronology(); 476 } else if (query == TemporalQuery.precision()) { 477 return (R) DAYS; 478 } 479 // inline TemporalAccessor.super.query(query) as an optimization 480 // non-JDK classes are not permitted to make this optimization 481 return query.queryFrom(this); 482 } 483 484 /** 485 * Adjusts the specified temporal object to have the same date as this object. 486 * <p> 487 * This returns a temporal object of the same observable type as the input 488 * with the date changed to be the same as this. 489 * <p> 490 * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)} 491 * passing {@link ChronoField#EPOCH_DAY} as the field. 492 * <p> 493 * In most cases, it is clearer to reverse the calling pattern by using 494 * {@link Temporal#with(TemporalAdjuster)}: 495 * <pre> 496 * // these two lines are equivalent, but the second approach is recommended 497 * temporal = thisLocalDate.adjustInto(temporal); 498 * temporal = temporal.with(thisLocalDate); 499 * </pre> 500 * <p> 501 * This instance is immutable and unaffected by this method call. 502 * 503 * @param temporal the target object to be adjusted, not null 504 * @return the adjusted object, not null 505 * @throws DateTimeException if unable to make the adjustment 506 * @throws ArithmeticException if numeric overflow occurs 507 */ 508 @Override 509 default Temporal adjustInto(Temporal temporal) { 510 return temporal.with(EPOCH_DAY, toEpochDay()); 511 } 512 513 /** 514 * Calculates the amount of time until another date in terms of the specified unit. 515 * <p> 516 * This calculates the amount of time between two {@code ChronoLocalDate} 517 * objects in terms of a single {@code TemporalUnit}. 518 * The start and end points are {@code this} and the specified date. 519 * The result will be negative if the end is before the start. 520 * The {@code Temporal} passed to this method must be a 521 * {@code ChronoLocalDate} in the same chronology. 522 * The calculation returns a whole number, representing the number of 523 * complete units between the two dates. 524 * For example, the amount in days between two dates can be calculated 525 * using {@code startDate.periodUntil(endDate, DAYS)}. 526 * <p> 527 * There are two equivalent ways of using this method. 528 * The first is to invoke this method. 529 * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}: 530 * <pre> 531 * // these two lines are equivalent 532 * amount = start.periodUntil(end, MONTHS); 533 * amount = MONTHS.between(start, end); 534 * </pre> 535 * The choice should be made based on which makes the code more readable. 536 * <p> 537 * The calculation is implemented in this method for {@link ChronoUnit}. 538 * The units {@code DAYS}, {@code WEEKS}, {@code MONTHS}, {@code YEARS}, 539 * {@code DECADES}, {@code CENTURIES}, {@code MILLENNIA} and {@code ERAS} 540 * should be supported by all implementations. 541 * Other {@code ChronoUnit} values will throw an exception. 542 * <p> 543 * If the unit is not a {@code ChronoUnit}, then the result of this method 544 * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)} 545 * passing {@code this} as the first argument and the input temporal as 546 * the second argument. 547 * <p> 548 * This instance is immutable and unaffected by this method call. 549 * 550 * @param endDate the end date, which must be a {@code ChronoLocalDate} 551 * in the same chronology, not null 552 * @param unit the unit to measure the amount in, not null 553 * @return the amount of time between this date and the end date 554 * @throws DateTimeException if the amount cannot be calculated 555 * @throws ArithmeticException if numeric overflow occurs 556 */ 557 @Override // override for Javadoc 558 long periodUntil(Temporal endDate, TemporalUnit unit); 559 560 /** 561 * Calculates the period between this date and another date as a {@code Period}. 562 * <p> 563 * This calculates the period between two dates in terms of years, months and days. 564 * The start and end points are {@code this} and the specified date. 565 * The result will be negative if the end is before the start. 566 * The negative sign will be the same in each of year, month and day. 567 * <p> 568 * The calculation is performed using the chronology of this date. 569 * If necessary, the input date will be converted to match. 570 * <p> 571 * This instance is immutable and unaffected by this method call. 572 * 573 * @param endDate the end date, exclusive, which may be in any chronology, not null 574 * @return the period between this date and the end date, not null 575 * @throws DateTimeException if the period cannot be calculated 576 * @throws ArithmeticException if numeric overflow occurs 577 */ 578 Period periodUntil(ChronoLocalDate<?> endDate); 579 580 /** 581 * Formats this date using the specified formatter. 582 * <p> 583 * This date will be passed to the formatter to produce a string. 584 * <p> 585 * The default implementation must behave as follows: 586 * <pre> 587 * return formatter.format(this); 588 * </pre> 589 * 590 * @param formatter the formatter to use, not null 591 * @return the formatted date string, not null 592 * @throws DateTimeException if an error occurs during printing 593 */ 594 default String format(DateTimeFormatter formatter) { 595 Objects.requireNonNull(formatter, "formatter"); 596 return formatter.format(this); 597 } 598 599 //----------------------------------------------------------------------- 600 /** 601 * Combines this date with a time to create a {@code ChronoLocalDateTime}. 602 * <p> 603 * This returns a {@code ChronoLocalDateTime} formed from this date at the specified time. 604 * All possible combinations of date and time are valid. 605 * 606 * @param localTime the local time to use, not null 607 * @return the local date-time formed from this date and the specified time, not null 608 */ 609 default ChronoLocalDateTime<D> atTime(LocalTime localTime) { 610 return (ChronoLocalDateTime<D>)ChronoLocalDateTimeImpl.of(this, localTime); 611 } 612 613 //----------------------------------------------------------------------- 614 /** 615 * Converts this date to the Epoch Day. 616 * <p> 617 * The {@link ChronoField#EPOCH_DAY Epoch Day count} is a simple 618 * incrementing count of days where day 0 is 1970-01-01 (ISO). 619 * This definition is the same for all chronologies, enabling conversion. 620 * <p> 621 * This default implementation queries the {@code EPOCH_DAY} field. 622 * 623 * @return the Epoch Day equivalent to this date 624 */ 625 default long toEpochDay() { 626 return getLong(EPOCH_DAY); 627 } 628 629 //----------------------------------------------------------------------- 630 /** 631 * Compares this date to another date, including the chronology. 632 * <p> 633 * The comparison is based first on the underlying time-line date, then 634 * on the chronology. 635 * It is "consistent with equals", as defined by {@link Comparable}. 636 * <p> 637 * For example, the following is the comparator order: 638 * <ol> 639 * <li>{@code 2012-12-03 (ISO)}</li> 640 * <li>{@code 2012-12-04 (ISO)}</li> 641 * <li>{@code 2555-12-04 (ThaiBuddhist)}</li> 642 * <li>{@code 2012-12-05 (ISO)}</li> 643 * </ol> 644 * Values #2 and #3 represent the same date on the time-line. 645 * When two values represent the same date, the chronology ID is compared to distinguish them. 646 * This step is needed to make the ordering "consistent with equals". 647 * <p> 648 * If all the date objects being compared are in the same chronology, then the 649 * additional chronology stage is not required and only the local date is used. 650 * To compare the dates of two {@code TemporalAccessor} instances, including dates 651 * in two different chronologies, use {@link ChronoField#EPOCH_DAY} as a comparator. 652 * <p> 653 * This default implementation performs the comparison defined above. 654 * 655 * @param other the other date to compare to, not null 656 * @return the comparator value, negative if less, positive if greater 657 */ 658 @Override 659 default int compareTo(ChronoLocalDate<?> other) { 660 int cmp = Long.compare(toEpochDay(), other.toEpochDay()); 661 if (cmp == 0) { 662 cmp = getChronology().compareTo(other.getChronology()); 663 } 664 return cmp; 665 } 666 667 /** 668 * Checks if this date is after the specified date ignoring the chronology. 669 * <p> 670 * This method differs from the comparison in {@link #compareTo} in that it 671 * only compares the underlying date and not the chronology. 672 * This allows dates in different calendar systems to be compared based 673 * on the time-line position. 674 * This is equivalent to using {@code date1.toEpochDay() > date2.toEpochDay()}. 675 * <p> 676 * This default implementation performs the comparison based on the epoch-day. 677 * 678 * @param other the other date to compare to, not null 679 * @return true if this is after the specified date 680 */ 681 default boolean isAfter(ChronoLocalDate<?> other) { 682 return this.toEpochDay() > other.toEpochDay(); 683 } 684 685 /** 686 * Checks if this date is before the specified date ignoring the chronology. 687 * <p> 688 * This method differs from the comparison in {@link #compareTo} in that it 689 * only compares the underlying date and not the chronology. 690 * This allows dates in different calendar systems to be compared based 691 * on the time-line position. 692 * This is equivalent to using {@code date1.toEpochDay() < date2.toEpochDay()}. 693 * <p> 694 * This default implementation performs the comparison based on the epoch-day. 695 * 696 * @param other the other date to compare to, not null 697 * @return true if this is before the specified date 698 */ 699 default boolean isBefore(ChronoLocalDate<?> other) { 700 return this.toEpochDay() < other.toEpochDay(); 701 } 702 703 /** 704 * Checks if this date is equal to the specified date ignoring the chronology. 705 * <p> 706 * This method differs from the comparison in {@link #compareTo} in that it 707 * only compares the underlying date and not the chronology. 708 * This allows dates in different calendar systems to be compared based 709 * on the time-line position. 710 * This is equivalent to using {@code date1.toEpochDay() == date2.toEpochDay()}. 711 * <p> 712 * This default implementation performs the comparison based on the epoch-day. 713 * 714 * @param other the other date to compare to, not null 715 * @return true if the underlying date is equal to the specified date 716 */ 717 default boolean isEqual(ChronoLocalDate<?> other) { 718 return this.toEpochDay() == other.toEpochDay(); 719 } 720 721 //----------------------------------------------------------------------- 722 /** 723 * Checks if this date is equal to another date, including the chronology. 724 * <p> 725 * Compares this date with another ensuring that the date and chronology are the same. 726 * <p> 727 * To compare the dates of two {@code TemporalAccessor} instances, including dates 728 * in two different chronologies, use {@link ChronoField#EPOCH_DAY} as a comparator. 729 * 730 * @param obj the object to check, null returns false 731 * @return true if this is equal to the other date 732 */ 733 @Override 734 boolean equals(Object obj); 735 736 /** 737 * A hash code for this date. 738 * 739 * @return a suitable hash code 740 */ 741 @Override 742 int hashCode(); 743 744 //----------------------------------------------------------------------- 745 /** 746 * Outputs this date as a {@code String}. 747 * <p> 748 * The output will include the full local date. 749 * 750 * @return the formatted date, not null 751 */ 752 @Override 753 String toString(); 754 755 }