src/share/classes/java/time/chrono/ThaiBuddhistDate.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 Thai Buddhist calendar system.

@@ -203,20 +204,50 @@
         Objects.requireNonNull(isoDate, "isoDate");
         this.isoDate = isoDate;
     }
 
     //-----------------------------------------------------------------------
+    /**
+     * Gets the chronology of this date, which is the Thai Buddhist 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 Thai Buddhist chronology, not null
+     */
     @Override
     public ThaiBuddhistChronology getChronology() {
         return ThaiBuddhistChronology.INSTANCE;
     }
 
+    /**
+     * Gets the era applicable at this date.
+     * <p>
+     * The Thai Buddhist calendar system has two eras, 'BE' and 'BEFORE_BE',
+     * defined by {@link ThaiBuddhistEra}.
+     *
+     * @return the era applicable at this date, not null
+     */
+    @Override
+    public ThaiBuddhistEra getEra() {
+        return (getProlepticYear() >= 1 ? ThaiBuddhistEra.BE : ThaiBuddhistEra.BEFORE_BE);
+    }
+
+    /**
+     * 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 : 1 - nvalue)  - YEARS_DIFFERENCE));
                         case YEAR:
                             return with(isoDate.withYear(nvalue - YEARS_DIFFERENCE));

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

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