src/share/classes/java/time/chrono/ChronoDateImpl.java

Print this page

        

*** 52,75 **** * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ ! package java.time.calendar; import static java.time.temporal.ChronoField.DAY_OF_MONTH; import static java.time.temporal.ChronoField.ERA; import static java.time.temporal.ChronoField.MONTH_OF_YEAR; import static java.time.temporal.ChronoField.YEAR_OF_ERA; import java.io.Serializable; import java.time.DateTimeException; import java.time.LocalDate; import java.time.LocalTime; ! import java.time.temporal.Chrono; ! import java.time.temporal.ChronoLocalDate; ! import java.time.temporal.ChronoLocalDateTime; import java.time.temporal.ChronoUnit; import java.time.temporal.Temporal; import java.time.temporal.TemporalAdjuster; import java.time.temporal.TemporalUnit; --- 52,75 ---- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ ! package java.time.chrono; import static java.time.temporal.ChronoField.DAY_OF_MONTH; import static java.time.temporal.ChronoField.ERA; import static java.time.temporal.ChronoField.MONTH_OF_YEAR; import static java.time.temporal.ChronoField.YEAR_OF_ERA; import java.io.Serializable; import java.time.DateTimeException; import java.time.LocalDate; import java.time.LocalTime; ! import java.time.chrono.Chronology; ! import java.time.chrono.ChronoLocalDate; ! import java.time.chrono.ChronoLocalDateTime; import java.time.temporal.ChronoUnit; import java.time.temporal.Temporal; import java.time.temporal.TemporalAdjuster; import java.time.temporal.TemporalUnit;
*** 78,88 **** * <p> * This class is used by applications seeking to handle dates in non-ISO calendar systems. * For example, the Japanese, Minguo, Thai Buddhist and others. * <p> * {@code ChronoLocalDate} is built on the generic concepts of year, month and day. ! * The calendar system, represented by a {@link java.time.temporal.Chrono}, expresses the relationship between * the fields and this class allows the resulting date to be manipulated. * <p> * Note that not all calendar systems are suitable for use with this class. * For example, the Mayan calendar uses a system that bears no relation to years, months and days. * <p> --- 78,88 ---- * <p> * This class is used by applications seeking to handle dates in non-ISO calendar systems. * For example, the Japanese, Minguo, Thai Buddhist and others. * <p> * {@code ChronoLocalDate} is built on the generic concepts of year, month and day. ! * The calendar system, represented by a {@link java.time.chrono.Chronology}, expresses the relationship between * the fields and this class allows the resulting date to be manipulated. * <p> * Note that not all calendar systems are suitable for use with this class. * For example, the Mayan calendar uses a system that bears no relation to years, months and days. * <p>
*** 93,148 **** * * <P>Example: </p> * <pre> * System.out.printf("Example()%n"); * // Enumerate the list of available calendars and print today for each ! * Set&lt;Chrono&gt; chronos = Chrono.getAvailableChronologies(); ! * for (Chrono chrono : chronos) { * ChronoLocalDate<?> date = chrono.dateNow(); * System.out.printf(" %20s: %s%n", chrono.getID(), date.toString()); * } * * // Print the Hijrah date and calendar ! * ChronoLocalDate<?> date = Chrono.of("Hijrah").dateNow(); * int day = date.get(ChronoField.DAY_OF_MONTH); * int dow = date.get(ChronoField.DAY_OF_WEEK); * int month = date.get(ChronoField.MONTH_OF_YEAR); * int year = date.get(ChronoField.YEAR); ! * System.out.printf(" Today is %s %s %d-%s-%d%n", date.getChrono().getID(), * dow, day, month, year); * // Print today's date and the last day of the year ! * ChronoLocalDate<?> now1 = Chrono.of("Hijrah").dateNow(); * ChronoLocalDate<?> first = now1.with(ChronoField.DAY_OF_MONTH, 1) * .with(ChronoField.MONTH_OF_YEAR, 1); * ChronoLocalDate<?> last = first.plus(1, ChronoUnit.YEARS) * .minus(1, ChronoUnit.DAYS); ! * System.out.printf(" Today is %s: start: %s; end: %s%n", last.getChrono().getID(), * first, last); * </pre> * * <h3>Adding Calendars</h3> * <p> The set of calendars is extensible by defining a subclass of {@link ChronoLocalDate} ! * to represent a date instance and an implementation of {@code Chrono} * to be the factory for the ChronoLocalDate subclass. * </p> * <p> To permit the discovery of the additional calendar types the implementation of ! * {@code Chrono} must be registered as a Service implementing the {@code Chrono} interface * in the {@code META-INF/Services} file as per the specification of {@link java.util.ServiceLoader}. ! * The subclass must function according to the {@code Chrono} class description and must provide its ! * {@link java.time.temporal.Chrono#getId() chronlogy ID} and {@link Chrono#getCalendarType() calendar type}. </p> * * <h3>Specification for implementors</h3> * This abstract class must be implemented with care to ensure other classes operate correctly. * All implementations that can be instantiated must be final, immutable and thread-safe. * Subclasses should be Serializable wherever possible. * ! * @param <C> the chronology of this date * @since 1.8 */ ! abstract class ChronoDateImpl<C extends Chrono<C>> ! implements ChronoLocalDate<C>, Temporal, TemporalAdjuster, Serializable { /** * Serialization version. */ private static final long serialVersionUID = 6282433883239719096L; --- 93,148 ---- * * <P>Example: </p> * <pre> * System.out.printf("Example()%n"); * // Enumerate the list of available calendars and print today for each ! * Set&lt;Chronology&gt; chronos = Chronology.getAvailableChronologies(); ! * for (Chronology chrono : chronos) { * ChronoLocalDate<?> date = chrono.dateNow(); * System.out.printf(" %20s: %s%n", chrono.getID(), date.toString()); * } * * // Print the Hijrah date and calendar ! * ChronoLocalDate<?> date = Chronology.of("Hijrah").dateNow(); * int day = date.get(ChronoField.DAY_OF_MONTH); * int dow = date.get(ChronoField.DAY_OF_WEEK); * int month = date.get(ChronoField.MONTH_OF_YEAR); * int year = date.get(ChronoField.YEAR); ! * System.out.printf(" Today is %s %s %d-%s-%d%n", date.getChronology().getID(), * dow, day, month, year); * // Print today's date and the last day of the year ! * ChronoLocalDate<?> now1 = Chronology.of("Hijrah").dateNow(); * ChronoLocalDate<?> first = now1.with(ChronoField.DAY_OF_MONTH, 1) * .with(ChronoField.MONTH_OF_YEAR, 1); * ChronoLocalDate<?> last = first.plus(1, ChronoUnit.YEARS) * .minus(1, ChronoUnit.DAYS); ! * System.out.printf(" Today is %s: start: %s; end: %s%n", last.getChronology().getID(), * first, last); * </pre> * * <h3>Adding Calendars</h3> * <p> The set of calendars is extensible by defining a subclass of {@link ChronoLocalDate} ! * to represent a date instance and an implementation of {@code Chronology} * to be the factory for the ChronoLocalDate subclass. * </p> * <p> To permit the discovery of the additional calendar types the implementation of ! * {@code Chronology} must be registered as a Service implementing the {@code Chronology} interface * in the {@code META-INF/Services} file as per the specification of {@link java.util.ServiceLoader}. ! * The subclass must function according to the {@code Chronology} class description and must provide its ! * {@link java.time.chrono.Chronology#getId() chronlogy ID} and {@link Chronology#getCalendarType() calendar type}. </p> * * <h3>Specification for implementors</h3> * This abstract class must be implemented with care to ensure other classes operate correctly. * All implementations that can be instantiated must be final, immutable and thread-safe. * Subclasses should be Serializable wherever possible. * ! * @param <D> the ChronoLocalDate of this date-time * @since 1.8 */ ! abstract class ChronoDateImpl<D extends ChronoLocalDate<D>> ! implements ChronoLocalDate<D>, Temporal, TemporalAdjuster, Serializable { /** * Serialization version. */ private static final long serialVersionUID = 6282433883239719096L;
*** 153,163 **** ChronoDateImpl() { } //----------------------------------------------------------------------- @Override ! public ChronoLocalDate<C> plus(long amountToAdd, TemporalUnit unit) { if (unit instanceof ChronoUnit) { ChronoUnit f = (ChronoUnit) unit; switch (f) { case DAYS: return plusDays(amountToAdd); case WEEKS: return plusDays(Math.multiplyExact(amountToAdd, 7)); --- 153,163 ---- ChronoDateImpl() { } //----------------------------------------------------------------------- @Override ! public D plus(long amountToAdd, TemporalUnit unit) { if (unit instanceof ChronoUnit) { ChronoUnit f = (ChronoUnit) unit; switch (f) { case DAYS: return plusDays(amountToAdd); case WEEKS: return plusDays(Math.multiplyExact(amountToAdd, 7));
*** 186,196 **** * * @param yearsToAdd the years to add, may be negative * @return a date based on this one with the years added, not null * @throws DateTimeException if the result exceeds the supported date range */ ! abstract ChronoDateImpl<C> plusYears(long yearsToAdd); /** * Returns a copy of this date with the specified period in months added. * <p> * This adds the specified period in months to the date. --- 186,196 ---- * * @param yearsToAdd the years to add, may be negative * @return a date based on this one with the years added, not null * @throws DateTimeException if the result exceeds the supported date range */ ! abstract D plusYears(long yearsToAdd); /** * Returns a copy of this date with the specified period in months added. * <p> * This adds the specified period in months to the date.
*** 202,212 **** * * @param monthsToAdd the months to add, may be negative * @return a date based on this one with the months added, not null * @throws DateTimeException if the result exceeds the supported date range */ ! abstract ChronoDateImpl<C> plusMonths(long monthsToAdd); /** * Returns a copy of this date with the specified period in weeks added. * <p> * This adds the specified period in weeks to the date. --- 202,212 ---- * * @param monthsToAdd the months to add, may be negative * @return a date based on this one with the months added, not null * @throws DateTimeException if the result exceeds the supported date range */ ! abstract D plusMonths(long monthsToAdd); /** * Returns a copy of this date with the specified period in weeks added. * <p> * This adds the specified period in weeks to the date.
*** 219,229 **** * * @param weeksToAdd the weeks to add, may be negative * @return a date based on this one with the weeks added, not null * @throws DateTimeException if the result exceeds the supported date range */ ! ChronoDateImpl<C> plusWeeks(long weeksToAdd) { return plusDays(Math.multiplyExact(weeksToAdd, 7)); } /** * Returns a copy of this date with the specified number of days added. --- 219,229 ---- * * @param weeksToAdd the weeks to add, may be negative * @return a date based on this one with the weeks added, not null * @throws DateTimeException if the result exceeds the supported date range */ ! D plusWeeks(long weeksToAdd) { return plusDays(Math.multiplyExact(weeksToAdd, 7)); } /** * Returns a copy of this date with the specified number of days added.
*** 234,244 **** * * @param daysToAdd the days to add, may be negative * @return a date based on this one with the days added, not null * @throws DateTimeException if the result exceeds the supported date range */ ! abstract ChronoDateImpl<C> plusDays(long daysToAdd); //----------------------------------------------------------------------- /** * Returns a copy of this date with the specified period in years subtracted. * <p> --- 234,244 ---- * * @param daysToAdd the days to add, may be negative * @return a date based on this one with the days added, not null * @throws DateTimeException if the result exceeds the supported date range */ ! abstract D plusDays(long daysToAdd); //----------------------------------------------------------------------- /** * Returns a copy of this date with the specified period in years subtracted. * <p>
*** 253,264 **** * * @param yearsToSubtract the years to subtract, may be negative * @return a date based on this one with the years subtracted, not null * @throws DateTimeException if the result exceeds the supported date range */ ! ChronoDateImpl<C> minusYears(long yearsToSubtract) { ! return (yearsToSubtract == Long.MIN_VALUE ? plusYears(Long.MAX_VALUE).plusYears(1) : plusYears(-yearsToSubtract)); } /** * Returns a copy of this date with the specified period in months subtracted. * <p> --- 253,264 ---- * * @param yearsToSubtract the years to subtract, may be negative * @return a date based on this one with the years subtracted, not null * @throws DateTimeException if the result exceeds the supported date range */ ! D minusYears(long yearsToSubtract) { ! return (yearsToSubtract == Long.MIN_VALUE ? ((ChronoDateImpl<D>)plusYears(Long.MAX_VALUE)).plusYears(1) : plusYears(-yearsToSubtract)); } /** * Returns a copy of this date with the specified period in months subtracted. * <p>
*** 273,284 **** * * @param monthsToSubtract the months to subtract, may be negative * @return a date based on this one with the months subtracted, not null * @throws DateTimeException if the result exceeds the supported date range */ ! ChronoDateImpl<C> minusMonths(long monthsToSubtract) { ! return (monthsToSubtract == Long.MIN_VALUE ? plusMonths(Long.MAX_VALUE).plusMonths(1) : plusMonths(-monthsToSubtract)); } /** * Returns a copy of this date with the specified period in weeks subtracted. * <p> --- 273,284 ---- * * @param monthsToSubtract the months to subtract, may be negative * @return a date based on this one with the months subtracted, not null * @throws DateTimeException if the result exceeds the supported date range */ ! D minusMonths(long monthsToSubtract) { ! return (monthsToSubtract == Long.MIN_VALUE ? ((ChronoDateImpl<D>)plusMonths(Long.MAX_VALUE)).plusMonths(1) : plusMonths(-monthsToSubtract)); } /** * Returns a copy of this date with the specified period in weeks subtracted. * <p>
*** 292,303 **** * * @param weeksToSubtract the weeks to subtract, may be negative * @return a date based on this one with the weeks subtracted, not null * @throws DateTimeException if the result exceeds the supported date range */ ! ChronoDateImpl<C> minusWeeks(long weeksToSubtract) { ! return (weeksToSubtract == Long.MIN_VALUE ? plusWeeks(Long.MAX_VALUE).plusWeeks(1) : plusWeeks(-weeksToSubtract)); } /** * Returns a copy of this date with the specified number of days subtracted. * <p> --- 292,303 ---- * * @param weeksToSubtract the weeks to subtract, may be negative * @return a date based on this one with the weeks subtracted, not null * @throws DateTimeException if the result exceeds the supported date range */ ! D minusWeeks(long weeksToSubtract) { ! return (weeksToSubtract == Long.MIN_VALUE ? ((ChronoDateImpl<D>)plusWeeks(Long.MAX_VALUE)).plusWeeks(1) : plusWeeks(-weeksToSubtract)); } /** * Returns a copy of this date with the specified number of days subtracted. * <p>
*** 309,325 **** * * @param daysToSubtract the days to subtract, may be negative * @return a date based on this one with the days subtracted, not null * @throws DateTimeException if the result exceeds the supported date range */ ! ChronoDateImpl<C> minusDays(long daysToSubtract) { ! return (daysToSubtract == Long.MIN_VALUE ? plusDays(Long.MAX_VALUE).plusDays(1) : plusDays(-daysToSubtract)); ! } ! ! @Override ! public final ChronoLocalDateTime<C> atTime(LocalTime localTime) { ! return Chrono.dateTime(this, localTime); } //----------------------------------------------------------------------- /** * {@inheritDoc} --- 309,320 ---- * * @param daysToSubtract the days to subtract, may be negative * @return a date based on this one with the days subtracted, not null * @throws DateTimeException if the result exceeds the supported date range */ ! D minusDays(long daysToSubtract) { ! return (daysToSubtract == Long.MIN_VALUE ? ((ChronoDateImpl<D>)plusDays(Long.MAX_VALUE)).plusDays(1) : plusDays(-daysToSubtract)); } //----------------------------------------------------------------------- /** * {@inheritDoc}
*** 330,346 **** public long periodUntil(Temporal endDateTime, TemporalUnit unit) { if (endDateTime instanceof ChronoLocalDate == false) { throw new DateTimeException("Unable to calculate period between objects of two different types"); } ChronoLocalDate<?> end = (ChronoLocalDate<?>) endDateTime; ! if (getChrono().equals(end.getChrono()) == false) { throw new DateTimeException("Unable to calculate period between two different chronologies"); } if (unit instanceof ChronoUnit) { return LocalDate.from(this).periodUntil(end, unit); // TODO: this is wrong } ! return unit.between(this, endDateTime).getAmount(); } @Override public boolean equals(Object obj) { if (this == obj) { --- 325,341 ---- public long periodUntil(Temporal endDateTime, TemporalUnit unit) { if (endDateTime instanceof ChronoLocalDate == false) { throw new DateTimeException("Unable to calculate period between objects of two different types"); } ChronoLocalDate<?> end = (ChronoLocalDate<?>) endDateTime; ! if (getChronology().equals(end.getChronology()) == false) { throw new DateTimeException("Unable to calculate period between two different chronologies"); } if (unit instanceof ChronoUnit) { return LocalDate.from(this).periodUntil(end, unit); // TODO: this is wrong } ! return unit.between(this, endDateTime); } @Override public boolean equals(Object obj) { if (this == obj) {
*** 353,373 **** } @Override public int hashCode() { long epDay = toEpochDay(); ! return getChrono().hashCode() ^ ((int) (epDay ^ (epDay >>> 32))); } @Override public String toString() { // getLong() reduces chances of exceptions in toString() long yoe = getLong(YEAR_OF_ERA); long moy = getLong(MONTH_OF_YEAR); long dom = getLong(DAY_OF_MONTH); StringBuilder buf = new StringBuilder(30); ! buf.append(getChrono().toString()) .append(" ") .append(getEra()) .append(" ") .append(yoe) .append(moy < 10 ? "-0" : "-").append(moy) --- 348,368 ---- } @Override public int hashCode() { long epDay = toEpochDay(); ! return getChronology().hashCode() ^ ((int) (epDay ^ (epDay >>> 32))); } @Override public String toString() { // getLong() reduces chances of exceptions in toString() long yoe = getLong(YEAR_OF_ERA); long moy = getLong(MONTH_OF_YEAR); long dom = getLong(DAY_OF_MONTH); StringBuilder buf = new StringBuilder(30); ! buf.append(getChronology().toString()) .append(" ") .append(getEra()) .append(" ") .append(yoe) .append(moy < 10 ? "-0" : "-").append(moy)