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

Print this page




  55  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  56  */
  57 package java.time.chrono;
  58 
  59 import static java.time.chrono.MinguoChronology.YEARS_DIFFERENCE;
  60 import static java.time.temporal.ChronoField.DAY_OF_MONTH;
  61 import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
  62 import static java.time.temporal.ChronoField.YEAR;
  63 
  64 import java.io.DataInput;
  65 import java.io.DataOutput;
  66 import java.io.IOException;
  67 import java.io.Serializable;
  68 import java.time.Clock;
  69 import java.time.DateTimeException;
  70 import java.time.LocalDate;
  71 import java.time.LocalTime;
  72 import java.time.Period;
  73 import java.time.ZoneId;
  74 import java.time.temporal.ChronoField;
  75 import java.time.temporal.TemporalQuery;
  76 import java.time.temporal.TemporalAccessor;
  77 import java.time.temporal.TemporalAdjuster;
  78 import java.time.temporal.TemporalAmount;
  79 import java.time.temporal.TemporalField;

  80 import java.time.temporal.TemporalUnit;

  81 import java.time.temporal.ValueRange;
  82 import java.util.Objects;
  83 
  84 /**
  85  * A date in the Minguo calendar system.
  86  * <p>
  87  * This date operates using the {@linkplain MinguoChronology Minguo calendar}.
  88  * This calendar system is primarily used in the Republic of China, often known as Taiwan.
  89  * Dates are aligned such that {@code 0001-01-01 (Minguo)} is {@code 1912-01-01 (ISO)}.
  90  *
  91  * <h3>Specification for implementors</h3>
  92  * This class is immutable and thread-safe.
  93  *
  94  * @since 1.8
  95  */
  96 public final class MinguoDate
  97         extends ChronoDateImpl<MinguoDate>
  98         implements ChronoLocalDate<MinguoDate>, Serializable {
  99 
 100     /**


 188      * @param temporal  the temporal object to convert, not null
 189      * @return the date in Minguo calendar system, not null
 190      * @throws DateTimeException if unable to convert to a {@code MinguoDate}
 191      */
 192     public static MinguoDate from(TemporalAccessor temporal) {
 193         return MinguoChronology.INSTANCE.date(temporal);
 194     }
 195 
 196     //-----------------------------------------------------------------------
 197     /**
 198      * Creates an instance from an ISO date.
 199      *
 200      * @param isoDate  the standard local date, validated not null
 201      */
 202     MinguoDate(LocalDate isoDate) {
 203         Objects.requireNonNull(isoDate, "isoDate");
 204         this.isoDate = isoDate;
 205     }
 206 
 207     //-----------------------------------------------------------------------








 208     @Override
 209     public MinguoChronology getChronology() {
 210         return MinguoChronology.INSTANCE;
 211     }
 212 





















 213     @Override
 214     public int lengthOfMonth() {
 215         return isoDate.lengthOfMonth();
 216     }
 217 

 218     @Override
 219     public ValueRange range(TemporalField field) {
 220         if (field instanceof ChronoField) {
 221             if (isSupported(field)) {
 222                 ChronoField f = (ChronoField) field;
 223                 switch (f) {
 224                     case DAY_OF_MONTH:
 225                     case DAY_OF_YEAR:
 226                     case ALIGNED_WEEK_OF_MONTH:
 227                         return isoDate.range(field);
 228                     case YEAR_OF_ERA: {
 229                         ValueRange range = YEAR.range();
 230                         long max = (getProlepticYear() <= 0 ? -range.getMinimum() + 1 + YEARS_DIFFERENCE : range.getMaximum() - YEARS_DIFFERENCE);
 231                         return ValueRange.of(1, max);
 232                     }
 233                 }
 234                 return getChronology().range(f);
 235             }
 236             throw new DateTimeException("Unsupported field: " + field.getName());
 237         }
 238         return field.rangeRefinedBy(this);
 239     }
 240 
 241     @Override
 242     public long getLong(TemporalField field) {
 243         if (field instanceof ChronoField) {
 244             switch ((ChronoField) field) {


 245                 case YEAR_OF_ERA: {
 246                     int prolepticYear = getProlepticYear();
 247                     return (prolepticYear >= 1 ? prolepticYear : 1 - prolepticYear);
 248                 }
 249                 case YEAR:
 250                     return getProlepticYear();
 251                 case ERA:
 252                     return (getProlepticYear() >= 1 ? 1 : 0);
 253             }
 254             return isoDate.getLong(field);
 255         }
 256         return field.getFrom(this);
 257     }
 258 




 259     private int getProlepticYear() {
 260         return isoDate.getYear() - YEARS_DIFFERENCE;
 261     }
 262 
 263     //-----------------------------------------------------------------------
 264     @Override
 265     public MinguoDate with(TemporalField field, long newValue) {
 266         if (field instanceof ChronoField) {
 267             ChronoField f = (ChronoField) field;
 268             if (getLong(f) == newValue) {
 269                 return this;
 270             }
 271             switch (f) {



 272                 case YEAR_OF_ERA:
 273                 case YEAR:
 274                 case ERA: {
 275                     f.checkValidValue(newValue);
 276                     int nvalue = (int) newValue;
 277                     switch (f) {
 278                         case YEAR_OF_ERA:
 279                             return with(isoDate.withYear(getProlepticYear() >= 1 ? nvalue + YEARS_DIFFERENCE : (1 - nvalue)  + YEARS_DIFFERENCE));
 280                         case YEAR:
 281                             return with(isoDate.withYear(nvalue + YEARS_DIFFERENCE));
 282                         case ERA:
 283                             return with(isoDate.withYear((1 - getProlepticYear()) + YEARS_DIFFERENCE));
 284                     }
 285                 }
 286             }
 287             return with(isoDate.with(field, newValue));
 288         }
 289         return (MinguoDate) ChronoLocalDate.super.with(field, newValue);
 290     }
 291 
 292     /**
 293      * {@inheritDoc}
 294      * @throws DateTimeException {@inheritDoc}
 295      * @throws ArithmeticException {@inheritDoc}
 296      */
 297     @Override
 298     public  MinguoDate with(TemporalAdjuster adjuster) {
 299         return (MinguoDate)super.with(adjuster);
 300     }
 301 
 302     /**
 303      * {@inheritDoc}
 304      * @throws DateTimeException {@inheritDoc}
 305      * @throws ArithmeticException {@inheritDoc}
 306      */
 307     @Override
 308     public MinguoDate plus(TemporalAmount amount) {
 309         return (MinguoDate)super.plus(amount);
 310     }
 311 
 312     /**
 313      * {@inheritDoc}
 314      * @throws DateTimeException {@inheritDoc}
 315      * @throws ArithmeticException {@inheritDoc}
 316      */
 317     @Override
 318     public MinguoDate minus(TemporalAmount amount) {
 319         return (MinguoDate)super.minus(amount);
 320     }
 321 
 322     //-----------------------------------------------------------------------
 323     @Override
 324     MinguoDate plusYears(long years) {
 325         return with(isoDate.plusYears(years));
 326     }
 327 
 328     @Override
 329     MinguoDate plusMonths(long months) {
 330         return with(isoDate.plusMonths(months));
 331     }
 332 
 333     @Override
 334     MinguoDate plusDays(long days) {
 335         return with(isoDate.plusDays(days));
 336     }
 337 
 338     @Override
 339     public MinguoDate plus(long amountToAdd, TemporalUnit unit) {
 340         return (MinguoDate)super.plus(amountToAdd, unit);
 341     }
 342 
 343     @Override
 344     public MinguoDate minus(long amountToAdd, TemporalUnit unit) {
 345         return (MinguoDate)super.minus(amountToAdd, unit);
 346     }
 347 
 348     @Override
 349     MinguoDate plusWeeks(long weeksToAdd) {
 350         return (MinguoDate)super.plusWeeks(weeksToAdd);
 351     }
 352 
 353     @Override
 354     MinguoDate minusYears(long yearsToSubtract) {
 355         return (MinguoDate)super.minusYears(yearsToSubtract);
 356     }
 357 
 358     @Override
 359     MinguoDate minusMonths(long monthsToSubtract) {
 360         return (MinguoDate)super.minusMonths(monthsToSubtract);
 361     }
 362 
 363     @Override
 364     MinguoDate minusWeeks(long weeksToSubtract) {
 365         return (MinguoDate)super.minusWeeks(weeksToSubtract);
 366     }
 367 
 368     @Override
 369     MinguoDate minusDays(long daysToSubtract) {
 370         return (MinguoDate)super.minusDays(daysToSubtract);
 371     }
 372 
 373     private MinguoDate with(LocalDate newDate) {
 374         return (newDate.equals(isoDate) ? this : new MinguoDate(newDate));
 375     }
 376 
 377     @Override        // for javadoc and covariant return type
 378     public final ChronoLocalDateTime<MinguoDate> atTime(LocalTime localTime) {
 379         return (ChronoLocalDateTime<MinguoDate>)super.atTime(localTime);
 380     }
 381 
 382     @Override
 383     public Period periodUntil(ChronoLocalDate<?> endDate) {
 384         return isoDate.periodUntil(endDate);
 385     }
 386 
 387     @Override  // override for performance
 388     public long toEpochDay() {
 389         return isoDate.toEpochDay();
 390     }
 391 
 392     //-------------------------------------------------------------------------
 393     @Override  // override for performance
 394     public boolean equals(Object obj) {
 395         if (this == obj) {
 396             return true;
 397         }
 398         if (obj instanceof MinguoDate) {
 399             MinguoDate otherDate = (MinguoDate) obj;


 402         return false;
 403     }
 404 
 405     @Override  // override for performance
 406     public int hashCode() {
 407         return getChronology().getId().hashCode() ^ isoDate.hashCode();
 408     }
 409 
 410     //-----------------------------------------------------------------------
 411     private Object writeReplace() {
 412         return new Ser(Ser.MINGUO_DATE_TYPE, this);
 413     }
 414 
 415     void writeExternal(DataOutput out) throws IOException {
 416         // MinguoChronology is implicit in the MINGUO_DATE_TYPE
 417         out.writeInt(get(YEAR));
 418         out.writeByte(get(MONTH_OF_YEAR));
 419         out.writeByte(get(DAY_OF_MONTH));
 420     }
 421 
 422     static ChronoLocalDate readExternal(DataInput in) throws IOException {
 423         int year = in.readInt();
 424         int month = in.readByte();
 425         int dayOfMonth = in.readByte();
 426         return MinguoChronology.INSTANCE.date(year, month, dayOfMonth);
 427     }
 428 
 429 }


  55  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  56  */
  57 package java.time.chrono;
  58 
  59 import static java.time.chrono.MinguoChronology.YEARS_DIFFERENCE;
  60 import static java.time.temporal.ChronoField.DAY_OF_MONTH;
  61 import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
  62 import static java.time.temporal.ChronoField.YEAR;
  63 
  64 import java.io.DataInput;
  65 import java.io.DataOutput;
  66 import java.io.IOException;
  67 import java.io.Serializable;
  68 import java.time.Clock;
  69 import java.time.DateTimeException;
  70 import java.time.LocalDate;
  71 import java.time.LocalTime;
  72 import java.time.Period;
  73 import java.time.ZoneId;
  74 import java.time.temporal.ChronoField;

  75 import java.time.temporal.TemporalAccessor;
  76 import java.time.temporal.TemporalAdjuster;
  77 import java.time.temporal.TemporalAmount;
  78 import java.time.temporal.TemporalField;
  79 import java.time.temporal.TemporalQuery;
  80 import java.time.temporal.TemporalUnit;
  81 import java.time.temporal.UnsupportedTemporalTypeException;
  82 import java.time.temporal.ValueRange;
  83 import java.util.Objects;
  84 
  85 /**
  86  * A date in the Minguo calendar system.
  87  * <p>
  88  * This date operates using the {@linkplain MinguoChronology Minguo calendar}.
  89  * This calendar system is primarily used in the Republic of China, often known as Taiwan.
  90  * Dates are aligned such that {@code 0001-01-01 (Minguo)} is {@code 1912-01-01 (ISO)}.
  91  *
  92  * <h3>Specification for implementors</h3>
  93  * This class is immutable and thread-safe.
  94  *
  95  * @since 1.8
  96  */
  97 public final class MinguoDate
  98         extends ChronoDateImpl<MinguoDate>
  99         implements ChronoLocalDate<MinguoDate>, Serializable {
 100 
 101     /**


 189      * @param temporal  the temporal object to convert, not null
 190      * @return the date in Minguo calendar system, not null
 191      * @throws DateTimeException if unable to convert to a {@code MinguoDate}
 192      */
 193     public static MinguoDate from(TemporalAccessor temporal) {
 194         return MinguoChronology.INSTANCE.date(temporal);
 195     }
 196 
 197     //-----------------------------------------------------------------------
 198     /**
 199      * Creates an instance from an ISO date.
 200      *
 201      * @param isoDate  the standard local date, validated not null
 202      */
 203     MinguoDate(LocalDate isoDate) {
 204         Objects.requireNonNull(isoDate, "isoDate");
 205         this.isoDate = isoDate;
 206     }
 207 
 208     //-----------------------------------------------------------------------
 209     /**
 210      * Gets the chronology of this date, which is the Minguo calendar system.
 211      * <p>
 212      * The {@code Chronology} represents the calendar system in use.
 213      * The era and other fields in {@link ChronoField} are defined by the chronology.
 214      *
 215      * @return the Minguo chronology, not null
 216      */
 217     @Override
 218     public MinguoChronology getChronology() {
 219         return MinguoChronology.INSTANCE;
 220     }
 221 
 222     /**
 223      * Gets the era applicable at this date.
 224      * <p>
 225      * The Minguo calendar system has two eras, 'ROC' and 'BEFORE_ROC',
 226      * defined by {@link MinguoEra}.
 227      *
 228      * @return the era applicable at this date, not null
 229      */
 230     @Override
 231     public MinguoEra getEra() {
 232         return (getProlepticYear() >= 1 ? MinguoEra.ROC : MinguoEra.BEFORE_ROC);
 233     }
 234 
 235     /**
 236      * Returns the length of the month represented by this date.
 237      * <p>
 238      * This returns the length of the month in days.
 239      * Month lengths match those of the ISO calendar system.
 240      *
 241      * @return the length of the month in days
 242      */
 243     @Override
 244     public int lengthOfMonth() {
 245         return isoDate.lengthOfMonth();
 246     }
 247 
 248     //-----------------------------------------------------------------------
 249     @Override
 250     public ValueRange range(TemporalField field) {
 251         if (field instanceof ChronoField) {
 252             if (isSupported(field)) {
 253                 ChronoField f = (ChronoField) field;
 254                 switch (f) {
 255                     case DAY_OF_MONTH:
 256                     case DAY_OF_YEAR:
 257                     case ALIGNED_WEEK_OF_MONTH:
 258                         return isoDate.range(field);
 259                     case YEAR_OF_ERA: {
 260                         ValueRange range = YEAR.range();
 261                         long max = (getProlepticYear() <= 0 ? -range.getMinimum() + 1 + YEARS_DIFFERENCE : range.getMaximum() - YEARS_DIFFERENCE);
 262                         return ValueRange.of(1, max);
 263                     }
 264                 }
 265                 return getChronology().range(f);
 266             }
 267             throw new UnsupportedTemporalTypeException("Unsupported field: " + field.getName());
 268         }
 269         return field.rangeRefinedBy(this);
 270     }
 271 
 272     @Override
 273     public long getLong(TemporalField field) {
 274         if (field instanceof ChronoField) {
 275             switch ((ChronoField) field) {
 276                 case PROLEPTIC_MONTH:
 277                     return getProlepticMonth();
 278                 case YEAR_OF_ERA: {
 279                     int prolepticYear = getProlepticYear();
 280                     return (prolepticYear >= 1 ? prolepticYear : 1 - prolepticYear);
 281                 }
 282                 case YEAR:
 283                     return getProlepticYear();
 284                 case ERA:
 285                     return (getProlepticYear() >= 1 ? 1 : 0);
 286             }
 287             return isoDate.getLong(field);
 288         }
 289         return field.getFrom(this);
 290     }
 291 
 292     private long getProlepticMonth() {
 293         return getProlepticYear() * 12L + isoDate.getMonthValue() - 1;
 294     }
 295 
 296     private int getProlepticYear() {
 297         return isoDate.getYear() - YEARS_DIFFERENCE;
 298     }
 299 
 300     //-----------------------------------------------------------------------
 301     @Override
 302     public MinguoDate with(TemporalField field, long newValue) {
 303         if (field instanceof ChronoField) {
 304             ChronoField f = (ChronoField) field;
 305             if (getLong(f) == newValue) {
 306                 return this;
 307             }
 308             switch (f) {
 309                 case PROLEPTIC_MONTH:
 310                     getChronology().range(f).checkValidValue(newValue, f);
 311                     return plusMonths(newValue - getProlepticMonth());
 312                 case YEAR_OF_ERA:
 313                 case YEAR:
 314                 case ERA: {
 315                     int nvalue = getChronology().range(f).checkValidIntValue(newValue, f);

 316                     switch (f) {
 317                         case YEAR_OF_ERA:
 318                             return with(isoDate.withYear(getProlepticYear() >= 1 ? nvalue + YEARS_DIFFERENCE : (1 - nvalue)  + YEARS_DIFFERENCE));
 319                         case YEAR:
 320                             return with(isoDate.withYear(nvalue + YEARS_DIFFERENCE));
 321                         case ERA:
 322                             return with(isoDate.withYear((1 - getProlepticYear()) + YEARS_DIFFERENCE));
 323                     }
 324                 }
 325             }
 326             return with(isoDate.with(field, newValue));
 327         }
 328         return ChronoLocalDate.super.with(field, newValue);
 329     }
 330 
 331     /**
 332      * {@inheritDoc}
 333      * @throws DateTimeException {@inheritDoc}
 334      * @throws ArithmeticException {@inheritDoc}
 335      */
 336     @Override
 337     public  MinguoDate with(TemporalAdjuster adjuster) {
 338         return super.with(adjuster);
 339     }
 340 
 341     /**
 342      * {@inheritDoc}
 343      * @throws DateTimeException {@inheritDoc}
 344      * @throws ArithmeticException {@inheritDoc}
 345      */
 346     @Override
 347     public MinguoDate plus(TemporalAmount amount) {
 348         return super.plus(amount);
 349     }
 350 
 351     /**
 352      * {@inheritDoc}
 353      * @throws DateTimeException {@inheritDoc}
 354      * @throws ArithmeticException {@inheritDoc}
 355      */
 356     @Override
 357     public MinguoDate minus(TemporalAmount amount) {
 358         return super.minus(amount);
 359     }
 360 
 361     //-----------------------------------------------------------------------
 362     @Override
 363     MinguoDate plusYears(long years) {
 364         return with(isoDate.plusYears(years));
 365     }
 366 
 367     @Override
 368     MinguoDate plusMonths(long months) {
 369         return with(isoDate.plusMonths(months));
 370     }
 371 
 372     @Override
 373     MinguoDate plusDays(long days) {
 374         return with(isoDate.plusDays(days));
 375     }
 376 
 377     @Override
 378     public MinguoDate plus(long amountToAdd, TemporalUnit unit) {
 379         return super.plus(amountToAdd, unit);
 380     }
 381 
 382     @Override
 383     public MinguoDate minus(long amountToAdd, TemporalUnit unit) {
 384         return super.minus(amountToAdd, unit);
 385     }
 386 
 387     @Override
 388     MinguoDate plusWeeks(long weeksToAdd) {
 389         return super.plusWeeks(weeksToAdd);
 390     }
 391 
 392     @Override
 393     MinguoDate minusYears(long yearsToSubtract) {
 394         return super.minusYears(yearsToSubtract);
 395     }
 396 
 397     @Override
 398     MinguoDate minusMonths(long monthsToSubtract) {
 399         return super.minusMonths(monthsToSubtract);
 400     }
 401 
 402     @Override
 403     MinguoDate minusWeeks(long weeksToSubtract) {
 404         return super.minusWeeks(weeksToSubtract);
 405     }
 406 
 407     @Override
 408     MinguoDate minusDays(long daysToSubtract) {
 409         return super.minusDays(daysToSubtract);
 410     }
 411 
 412     private MinguoDate with(LocalDate newDate) {
 413         return (newDate.equals(isoDate) ? this : new MinguoDate(newDate));
 414     }
 415 
 416     @Override        // for javadoc and covariant return type
 417     public final ChronoLocalDateTime<MinguoDate> atTime(LocalTime localTime) {
 418         return super.atTime(localTime);
 419     }
 420 
 421     @Override
 422     public Period periodUntil(ChronoLocalDate<?> endDate) {
 423         return isoDate.periodUntil(endDate);
 424     }
 425 
 426     @Override  // override for performance
 427     public long toEpochDay() {
 428         return isoDate.toEpochDay();
 429     }
 430 
 431     //-------------------------------------------------------------------------
 432     @Override  // override for performance
 433     public boolean equals(Object obj) {
 434         if (this == obj) {
 435             return true;
 436         }
 437         if (obj instanceof MinguoDate) {
 438             MinguoDate otherDate = (MinguoDate) obj;


 441         return false;
 442     }
 443 
 444     @Override  // override for performance
 445     public int hashCode() {
 446         return getChronology().getId().hashCode() ^ isoDate.hashCode();
 447     }
 448 
 449     //-----------------------------------------------------------------------
 450     private Object writeReplace() {
 451         return new Ser(Ser.MINGUO_DATE_TYPE, this);
 452     }
 453 
 454     void writeExternal(DataOutput out) throws IOException {
 455         // MinguoChronology is implicit in the MINGUO_DATE_TYPE
 456         out.writeInt(get(YEAR));
 457         out.writeByte(get(MONTH_OF_YEAR));
 458         out.writeByte(get(DAY_OF_MONTH));
 459     }
 460 
 461     static ChronoLocalDate<?> readExternal(DataInput in) throws IOException {
 462         int year = in.readInt();
 463         int month = in.readByte();
 464         int dayOfMonth = in.readByte();
 465         return MinguoChronology.INSTANCE.date(year, month, dayOfMonth);
 466     }
 467 
 468 }