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

Print this page

        

@@ -70,16 +70,17 @@
 import java.time.LocalDate;
 import java.time.LocalTime;
 import java.time.Period;
 import java.time.ZoneId;
 import java.time.temporal.ChronoField;
-import java.time.temporal.TemporalQuery;
 import java.time.temporal.TemporalAccessor;
 import java.time.temporal.TemporalAdjuster;
 import java.time.temporal.TemporalAmount;
 import java.time.temporal.TemporalField;
+import java.time.temporal.TemporalQuery;
 import java.time.temporal.TemporalUnit;
+import java.time.temporal.UnsupportedTemporalTypeException;
 import java.time.temporal.ValueRange;
 import java.util.Objects;
 
 /**
  * A date in the Minguo calendar system.

@@ -203,20 +204,50 @@
         Objects.requireNonNull(isoDate, "isoDate");
         this.isoDate = isoDate;
     }
 
     //-----------------------------------------------------------------------
+    /**
+     * Gets the chronology of this date, which is the Minguo calendar system.
+     * <p>
+     * The {@code Chronology} represents the calendar system in use.
+     * The era and other fields in {@link ChronoField} are defined by the chronology.
+     *
+     * @return the Minguo chronology, not null
+     */
     @Override
     public MinguoChronology getChronology() {
         return MinguoChronology.INSTANCE;
     }
 
+    /**
+     * Gets the era applicable at this date.
+     * <p>
+     * The Minguo calendar system has two eras, 'ROC' and 'BEFORE_ROC',
+     * defined by {@link MinguoEra}.
+     *
+     * @return the era applicable at this date, not null
+     */
+    @Override
+    public MinguoEra getEra() {
+        return (getProlepticYear() >= 1 ? MinguoEra.ROC : MinguoEra.BEFORE_ROC);
+    }
+
+    /**
+     * Returns the length of the month represented by this date.
+     * <p>
+     * This returns the length of the month in days.
+     * Month lengths match those of the ISO calendar system.
+     *
+     * @return the length of the month in days
+     */
     @Override
     public int lengthOfMonth() {
         return isoDate.lengthOfMonth();
     }
 
+    //-----------------------------------------------------------------------
     @Override
     public ValueRange range(TemporalField field) {
         if (field instanceof ChronoField) {
             if (isSupported(field)) {
                 ChronoField f = (ChronoField) field;

@@ -231,19 +262,21 @@
                         return ValueRange.of(1, max);
                     }
                 }
                 return getChronology().range(f);
             }
-            throw new DateTimeException("Unsupported field: " + field.getName());
+            throw new UnsupportedTemporalTypeException("Unsupported field: " + field.getName());
         }
         return field.rangeRefinedBy(this);
     }
 
     @Override
     public long getLong(TemporalField field) {
         if (field instanceof ChronoField) {
             switch ((ChronoField) field) {
+                case PROLEPTIC_MONTH:
+                    return getProlepticMonth();
                 case YEAR_OF_ERA: {
                     int prolepticYear = getProlepticYear();
                     return (prolepticYear >= 1 ? prolepticYear : 1 - prolepticYear);
                 }
                 case YEAR:

@@ -254,10 +287,14 @@
             return isoDate.getLong(field);
         }
         return field.getFrom(this);
     }
 
+    private long getProlepticMonth() {
+        return getProlepticYear() * 12L + isoDate.getMonthValue() - 1;
+    }
+
     private int getProlepticYear() {
         return isoDate.getYear() - YEARS_DIFFERENCE;
     }
 
     //-----------------------------------------------------------------------

@@ -267,15 +304,17 @@
             ChronoField f = (ChronoField) field;
             if (getLong(f) == newValue) {
                 return this;
             }
             switch (f) {
+                case PROLEPTIC_MONTH:
+                    getChronology().range(f).checkValidValue(newValue, f);
+                    return plusMonths(newValue - getProlepticMonth());
                 case YEAR_OF_ERA:
                 case YEAR:
                 case ERA: {
-                    f.checkValidValue(newValue);
-                    int nvalue = (int) newValue;
+                    int nvalue = getChronology().range(f).checkValidIntValue(newValue, f);
                     switch (f) {
                         case YEAR_OF_ERA:
                             return with(isoDate.withYear(getProlepticYear() >= 1 ? nvalue + YEARS_DIFFERENCE : (1 - nvalue)  + YEARS_DIFFERENCE));
                         case YEAR:
                             return with(isoDate.withYear(nvalue + YEARS_DIFFERENCE));

@@ -284,41 +323,41 @@
                     }
                 }
             }
             return with(isoDate.with(field, newValue));
         }
-        return (MinguoDate) ChronoLocalDate.super.with(field, newValue);
+        return ChronoLocalDate.super.with(field, newValue);
     }
 
     /**
      * {@inheritDoc}
      * @throws DateTimeException {@inheritDoc}
      * @throws ArithmeticException {@inheritDoc}
      */
     @Override
     public  MinguoDate with(TemporalAdjuster adjuster) {
-        return (MinguoDate)super.with(adjuster);
+        return super.with(adjuster);
     }
 
     /**
      * {@inheritDoc}
      * @throws DateTimeException {@inheritDoc}
      * @throws ArithmeticException {@inheritDoc}
      */
     @Override
     public MinguoDate plus(TemporalAmount amount) {
-        return (MinguoDate)super.plus(amount);
+        return super.plus(amount);
     }
 
     /**
      * {@inheritDoc}
      * @throws DateTimeException {@inheritDoc}
      * @throws ArithmeticException {@inheritDoc}
      */
     @Override
     public MinguoDate minus(TemporalAmount amount) {
-        return (MinguoDate)super.minus(amount);
+        return super.minus(amount);
     }
 
     //-----------------------------------------------------------------------
     @Override
     MinguoDate plusYears(long years) {

@@ -335,50 +374,50 @@
         return with(isoDate.plusDays(days));
     }
 
     @Override
     public MinguoDate plus(long amountToAdd, TemporalUnit unit) {
-        return (MinguoDate)super.plus(amountToAdd, unit);
+        return super.plus(amountToAdd, unit);
     }
 
     @Override
     public MinguoDate minus(long amountToAdd, TemporalUnit unit) {
-        return (MinguoDate)super.minus(amountToAdd, unit);
+        return super.minus(amountToAdd, unit);
     }
 
     @Override
     MinguoDate plusWeeks(long weeksToAdd) {
-        return (MinguoDate)super.plusWeeks(weeksToAdd);
+        return super.plusWeeks(weeksToAdd);
     }
 
     @Override
     MinguoDate minusYears(long yearsToSubtract) {
-        return (MinguoDate)super.minusYears(yearsToSubtract);
+        return super.minusYears(yearsToSubtract);
     }
 
     @Override
     MinguoDate minusMonths(long monthsToSubtract) {
-        return (MinguoDate)super.minusMonths(monthsToSubtract);
+        return super.minusMonths(monthsToSubtract);
     }
 
     @Override
     MinguoDate minusWeeks(long weeksToSubtract) {
-        return (MinguoDate)super.minusWeeks(weeksToSubtract);
+        return super.minusWeeks(weeksToSubtract);
     }
 
     @Override
     MinguoDate minusDays(long daysToSubtract) {
-        return (MinguoDate)super.minusDays(daysToSubtract);
+        return super.minusDays(daysToSubtract);
     }
 
     private MinguoDate with(LocalDate newDate) {
         return (newDate.equals(isoDate) ? this : new MinguoDate(newDate));
     }
 
     @Override        // for javadoc and covariant return type
     public final ChronoLocalDateTime<MinguoDate> atTime(LocalTime localTime) {
-        return (ChronoLocalDateTime<MinguoDate>)super.atTime(localTime);
+        return super.atTime(localTime);
     }
 
     @Override
     public Period periodUntil(ChronoLocalDate<?> endDate) {
         return isoDate.periodUntil(endDate);

@@ -417,11 +456,11 @@
         out.writeInt(get(YEAR));
         out.writeByte(get(MONTH_OF_YEAR));
         out.writeByte(get(DAY_OF_MONTH));
     }
 
-    static ChronoLocalDate readExternal(DataInput in) throws IOException {
+    static ChronoLocalDate<?> readExternal(DataInput in) throws IOException {
         int year = in.readInt();
         int month = in.readByte();
         int dayOfMonth = in.readByte();
         return MinguoChronology.INSTANCE.date(year, month, dayOfMonth);
     }