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

Print this page




  39  *    and/or other materials provided with the distribution.
  40  *
  41  *  * Neither the name of JSR-310 nor the names of its contributors
  42  *    may be used to endorse or promote products derived from this software
  43  *    without specific prior written permission.
  44  *
  45  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  46  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  47  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  48  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  49  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  50  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  51  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  52  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  53  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  54  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  55  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  56  */
  57 package java.time.chrono;
  58 

  59 import static java.time.temporal.ChronoField.YEAR;
  60 
  61 import java.io.Serializable;
  62 import java.time.Clock;
  63 import java.time.DateTimeException;
  64 import java.time.Instant;
  65 import java.time.LocalDate;
  66 import java.time.ZoneId;
  67 import java.time.temporal.ChronoField;
  68 import java.time.temporal.TemporalAccessor;
  69 import java.time.temporal.ValueRange;
  70 import java.util.Arrays;
  71 import java.util.List;
  72 import java.util.Locale;
  73 
  74 /**
  75  * The Minguo calendar system.
  76  * <p>
  77  * This chronology defines the rules of the Minguo calendar system.
  78  * This calendar system is primarily used in the Republic of China, often known as Taiwan.


  90  * <li>month-of-year - The Minguo month-of-year exactly matches ISO.
  91  * <li>day-of-month - The Minguo day-of-month exactly matches ISO.
  92  * <li>day-of-year - The Minguo day-of-year exactly matches ISO.
  93  * <li>leap-year - The Minguo leap-year pattern exactly matches ISO, such that the two calendars
  94  *  are never out of step.
  95  * </ul><p>
  96  *
  97  * <h3>Specification for implementors</h3>
  98  * This class is immutable and thread-safe.
  99  *
 100  * @since 1.8
 101  */
 102 public final class MinguoChronology extends Chronology implements Serializable {
 103 
 104     /**
 105      * Singleton instance for the Minguo chronology.
 106      */
 107     public static final MinguoChronology INSTANCE = new MinguoChronology();
 108 
 109     /**
 110      * The singleton instance for the era ROC.
 111      */
 112     public static final Era ERA_ROC = MinguoEra.ROC;
 113 
 114     /**
 115      * The singleton instance for the era BEFORE_ROC.
 116      */
 117     public static final Era ERA_BEFORE_ROC = MinguoEra.BEFORE_ROC;
 118 
 119     /**
 120      * Serialization version.
 121      */
 122     private static final long serialVersionUID = 1039765215346859963L;
 123     /**
 124      * The difference in years between ISO and Minguo.
 125      */
 126     static final int YEARS_DIFFERENCE = 1911;
 127 
 128     /**
 129      * Restricted constructor.
 130      */
 131     private MinguoChronology() {
 132     }
 133 
 134     /**
 135      * Resolve singleton.
 136      *
 137      * @return the singleton instance, not null
 138      */
 139     private Object readResolve() {
 140         return INSTANCE;
 141     }
 142 
 143     //-----------------------------------------------------------------------
 144     /**
 145      * Gets the ID of the chronology - 'Minguo'.
 146      * <p>
 147      * The ID uniquely identifies the {@code Chronology}.
 148      * It can be used to lookup the {@code Chronology} using {@link #of(String)}.
 149      *
 150      * @return the chronology ID - 'Minguo'
 151      * @see #getCalendarType()
 152      */
 153     @Override
 154     public String getId() {
 155         return "Minguo";
 156     }
 157 
 158     /**
 159      * Gets the calendar type of the underlying calendar system - 'roc'.
 160      * <p>
 161      * The calendar type is an identifier defined by the
 162      * <em>Unicode Locale Data Markup Language (LDML)</em> specification.
 163      * It can be used to lookup the {@code Chronology} using {@link #of(String)}.
 164      * It can also be used as part of a locale, accessible via
 165      * {@link Locale#getUnicodeLocaleType(String)} with the key 'ca'.
 166      *
 167      * @return the calendar system type - 'roc'
 168      * @see #getId()
 169      */
 170     @Override
 171     public String getCalendarType() {
 172         return "roc";
 173     }
 174 
 175     //-----------------------------------------------------------------------












 176     @Override
 177     public MinguoDate date(int prolepticYear, int month, int dayOfMonth) {
 178         return new MinguoDate(LocalDate.of(prolepticYear + YEARS_DIFFERENCE, month, dayOfMonth));
 179     }
 180 










 181     @Override
 182     public MinguoDate dateYearDay(int prolepticYear, int dayOfYear) {
 183         return new MinguoDate(LocalDate.ofYearDay(prolepticYear + YEARS_DIFFERENCE, dayOfYear));
 184     }
 185 











 186     @Override
 187     public MinguoDate date(TemporalAccessor temporal) {
 188         if (temporal instanceof MinguoDate) {
 189             return (MinguoDate) temporal;
 190         }
 191         return new MinguoDate(LocalDate.from(temporal));
 192     }
 193     @Override
 194     public MinguoDate date(Era era, int yearOfEra, int month, int dayOfMonth) {
 195         return date(prolepticYear(era, yearOfEra), month, dayOfMonth);
 196 












 197     }
 198 
 199     @Override
 200     public MinguoDate dateYearDay(Era era, int yearOfEra, int dayOfYear) {
 201         return dateYearDay(prolepticYear(era, yearOfEra), dayOfYear);







 202     }
 203 
 204     @Override
 205     public MinguoDate dateNow() {
 206         return dateNow(Clock.systemDefaultZone());
 207     }
 208 
 209     @Override
 210     public MinguoDate dateNow(ZoneId zone) {
 211         return dateNow(Clock.system(zone));
 212     }
 213 
 214     @Override
 215     public MinguoDate dateNow(Clock clock) {
 216         return date(LocalDate.now(clock));
 217     }
 218 
 219     @Override








 220     public ChronoLocalDateTime<MinguoDate> localDateTime(TemporalAccessor temporal) {
 221         return (ChronoLocalDateTime<MinguoDate>)super.localDateTime(temporal);
 222     }
 223 
 224     @Override
 225     public ChronoZonedDateTime<MinguoDate> zonedDateTime(TemporalAccessor temporal) {
 226         return (ChronoZonedDateTime<MinguoDate>)super.zonedDateTime(temporal);
 227     }
 228 
 229     @Override
 230     public ChronoZonedDateTime<MinguoDate> zonedDateTime(Instant instant, ZoneId zone) {
 231         return (ChronoZonedDateTime<MinguoDate>)super.zonedDateTime(instant, zone);
 232     }
 233 
 234     //-----------------------------------------------------------------------
 235     /**
 236      * Checks if the specified year is a leap year.
 237      * <p>
 238      * Minguo leap years occur exactly in line with ISO leap years.
 239      * This method does not validate the year passed in, and only has a
 240      * well-defined result for years in the supported range.
 241      *
 242      * @param prolepticYear  the proleptic-year to check, not validated for range
 243      * @return true if the year is a leap year
 244      */
 245     @Override
 246     public boolean isLeapYear(long prolepticYear) {
 247         return IsoChronology.INSTANCE.isLeapYear(prolepticYear + YEARS_DIFFERENCE);
 248     }
 249 
 250     @Override
 251     public int prolepticYear(Era era, int yearOfEra) {
 252         if (era instanceof MinguoEra == false) {
 253             throw new DateTimeException("Era must be MinguoEra");
 254         }
 255         return (era == MinguoEra.ROC ? yearOfEra : 1 - yearOfEra);
 256     }
 257 
 258     @Override
 259     public Era eraOf(int eraValue) {
 260         return MinguoEra.of(eraValue);
 261     }
 262 
 263     @Override
 264     public List<Era> eras() {
 265         return Arrays.<Era>asList(MinguoEra.values());
 266     }
 267 
 268     //-----------------------------------------------------------------------
 269     @Override
 270     public ValueRange range(ChronoField field) {
 271         switch (field) {




 272             case YEAR_OF_ERA: {
 273                 ValueRange range = YEAR.range();
 274                 return ValueRange.of(1, range.getMaximum() - YEARS_DIFFERENCE, -range.getMinimum() + 1 + YEARS_DIFFERENCE);
 275             }
 276             case YEAR: {
 277                 ValueRange range = YEAR.range();
 278                 return ValueRange.of(range.getMinimum() - YEARS_DIFFERENCE, range.getMaximum() - YEARS_DIFFERENCE);
 279             }
 280         }
 281         return field.range();
 282     }
 283 
 284 }


  39  *    and/or other materials provided with the distribution.
  40  *
  41  *  * Neither the name of JSR-310 nor the names of its contributors
  42  *    may be used to endorse or promote products derived from this software
  43  *    without specific prior written permission.
  44  *
  45  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  46  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  47  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  48  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  49  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  50  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  51  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  52  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  53  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  54  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  55  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  56  */
  57 package java.time.chrono;
  58 
  59 import static java.time.temporal.ChronoField.PROLEPTIC_MONTH;
  60 import static java.time.temporal.ChronoField.YEAR;
  61 
  62 import java.io.Serializable;
  63 import java.time.Clock;
  64 import java.time.DateTimeException;
  65 import java.time.Instant;
  66 import java.time.LocalDate;
  67 import java.time.ZoneId;
  68 import java.time.temporal.ChronoField;
  69 import java.time.temporal.TemporalAccessor;
  70 import java.time.temporal.ValueRange;
  71 import java.util.Arrays;
  72 import java.util.List;
  73 import java.util.Locale;
  74 
  75 /**
  76  * The Minguo calendar system.
  77  * <p>
  78  * This chronology defines the rules of the Minguo calendar system.
  79  * This calendar system is primarily used in the Republic of China, often known as Taiwan.


  91  * <li>month-of-year - The Minguo month-of-year exactly matches ISO.
  92  * <li>day-of-month - The Minguo day-of-month exactly matches ISO.
  93  * <li>day-of-year - The Minguo day-of-year exactly matches ISO.
  94  * <li>leap-year - The Minguo leap-year pattern exactly matches ISO, such that the two calendars
  95  *  are never out of step.
  96  * </ul><p>
  97  *
  98  * <h3>Specification for implementors</h3>
  99  * This class is immutable and thread-safe.
 100  *
 101  * @since 1.8
 102  */
 103 public final class MinguoChronology extends Chronology implements Serializable {
 104 
 105     /**
 106      * Singleton instance for the Minguo chronology.
 107      */
 108     public static final MinguoChronology INSTANCE = new MinguoChronology();
 109 
 110     /**










 111      * Serialization version.
 112      */
 113     private static final long serialVersionUID = 1039765215346859963L;
 114     /**
 115      * The difference in years between ISO and Minguo.
 116      */
 117     static final int YEARS_DIFFERENCE = 1911;
 118 
 119     /**
 120      * Restricted constructor.
 121      */
 122     private MinguoChronology() {
 123     }
 124 









 125     //-----------------------------------------------------------------------
 126     /**
 127      * Gets the ID of the chronology - 'Minguo'.
 128      * <p>
 129      * The ID uniquely identifies the {@code Chronology}.
 130      * It can be used to lookup the {@code Chronology} using {@link #of(String)}.
 131      *
 132      * @return the chronology ID - 'Minguo'
 133      * @see #getCalendarType()
 134      */
 135     @Override
 136     public String getId() {
 137         return "Minguo";
 138     }
 139 
 140     /**
 141      * Gets the calendar type of the underlying calendar system - 'roc'.
 142      * <p>
 143      * The calendar type is an identifier defined by the
 144      * <em>Unicode Locale Data Markup Language (LDML)</em> specification.
 145      * It can be used to lookup the {@code Chronology} using {@link #of(String)}.
 146      * It can also be used as part of a locale, accessible via
 147      * {@link Locale#getUnicodeLocaleType(String)} with the key 'ca'.
 148      *
 149      * @return the calendar system type - 'roc'
 150      * @see #getId()
 151      */
 152     @Override
 153     public String getCalendarType() {
 154         return "roc";
 155     }
 156 
 157     //-----------------------------------------------------------------------
 158     /**
 159      * Obtains a local date in Minguo calendar system from the
 160      * era, year-of-era, month-of-year and day-of-month fields.
 161      *
 162      * @param era  the Minguo era, not null
 163      * @param yearOfEra  the year-of-era
 164      * @param month  the month-of-year
 165      * @param dayOfMonth  the day-of-month
 166      * @return the Minguo local date, not null
 167      * @throws DateTimeException if unable to create the date
 168      * @throws ClassCastException if the {@code era} is not a {@code MinguoEra}
 169      */
 170     @Override
 171     public MinguoDate date(Era era, int yearOfEra, int month, int dayOfMonth) {
 172         return date(prolepticYear(era, yearOfEra), month, dayOfMonth);
 173     }
 174 
 175     /**
 176      * Obtains a local date in Minguo calendar system from the
 177      * proleptic-year, month-of-year and day-of-month fields.
 178      *
 179      * @param prolepticYear  the proleptic-year
 180      * @param month  the month-of-year
 181      * @param dayOfMonth  the day-of-month
 182      * @return the Minguo local date, not null
 183      * @throws DateTimeException if unable to create the date
 184      */
 185     @Override
 186     public MinguoDate date(int prolepticYear, int month, int dayOfMonth) {
 187         return new MinguoDate(LocalDate.of(prolepticYear + YEARS_DIFFERENCE, month, dayOfMonth));
 188     }
 189 
 190     /**
 191      * Obtains a local date in Minguo calendar system from the
 192      * era, year-of-era and day-of-year fields.
 193      *
 194      * @param era  the Minguo era, not null
 195      * @param yearOfEra  the year-of-era
 196      * @param dayOfYear  the day-of-year
 197      * @return the Minguo local date, not null
 198      * @throws DateTimeException if unable to create the date
 199      * @throws ClassCastException if the {@code era} is not a {@code MinguoEra}
 200      */
 201     @Override
 202     public MinguoDate dateYearDay(Era era, int yearOfEra, int dayOfYear) {
 203         return dateYearDay(prolepticYear(era, yearOfEra), dayOfYear);



 204     }



 205 
 206     /**
 207      * Obtains a local date in Minguo calendar system from the
 208      * proleptic-year and day-of-year fields.
 209      *
 210      * @param prolepticYear  the proleptic-year
 211      * @param dayOfYear  the day-of-year
 212      * @return the Minguo local date, not null
 213      * @throws DateTimeException if unable to create the date
 214      */
 215     @Override
 216     public MinguoDate dateYearDay(int prolepticYear, int dayOfYear) {
 217         return new MinguoDate(LocalDate.ofYearDay(prolepticYear + YEARS_DIFFERENCE, dayOfYear));
 218     }
 219 
 220     /**
 221      * Obtains a local date in the Minguo calendar system from the epoch-day.
 222      *
 223      * @param epochDay  the epoch day
 224      * @return the Minguo local date, not null
 225      * @throws DateTimeException if unable to create the date
 226      */
 227     @Override  // override with covariant return type
 228     public MinguoDate dateEpochDay(long epochDay) {
 229         return new MinguoDate(LocalDate.ofEpochDay(epochDay));
 230     }
 231 
 232     @Override
 233     public MinguoDate dateNow() {
 234         return dateNow(Clock.systemDefaultZone());
 235     }
 236 
 237     @Override
 238     public MinguoDate dateNow(ZoneId zone) {
 239         return dateNow(Clock.system(zone));
 240     }
 241 
 242     @Override
 243     public MinguoDate dateNow(Clock clock) {
 244         return date(LocalDate.now(clock));
 245     }
 246 
 247     @Override
 248     public MinguoDate date(TemporalAccessor temporal) {
 249         if (temporal instanceof MinguoDate) {
 250             return (MinguoDate) temporal;
 251         }
 252         return new MinguoDate(LocalDate.from(temporal));
 253     }
 254 
 255     @Override
 256     public ChronoLocalDateTime<MinguoDate> localDateTime(TemporalAccessor temporal) {
 257         return (ChronoLocalDateTime<MinguoDate>)super.localDateTime(temporal);
 258     }
 259 
 260     @Override
 261     public ChronoZonedDateTime<MinguoDate> zonedDateTime(TemporalAccessor temporal) {
 262         return (ChronoZonedDateTime<MinguoDate>)super.zonedDateTime(temporal);
 263     }
 264 
 265     @Override
 266     public ChronoZonedDateTime<MinguoDate> zonedDateTime(Instant instant, ZoneId zone) {
 267         return (ChronoZonedDateTime<MinguoDate>)super.zonedDateTime(instant, zone);
 268     }
 269 
 270     //-----------------------------------------------------------------------
 271     /**
 272      * Checks if the specified year is a leap year.
 273      * <p>
 274      * Minguo leap years occur exactly in line with ISO leap years.
 275      * This method does not validate the year passed in, and only has a
 276      * well-defined result for years in the supported range.
 277      *
 278      * @param prolepticYear  the proleptic-year to check, not validated for range
 279      * @return true if the year is a leap year
 280      */
 281     @Override
 282     public boolean isLeapYear(long prolepticYear) {
 283         return IsoChronology.INSTANCE.isLeapYear(prolepticYear + YEARS_DIFFERENCE);
 284     }
 285 
 286     @Override
 287     public int prolepticYear(Era era, int yearOfEra) {
 288         if (era instanceof MinguoEra == false) {
 289             throw new ClassCastException("Era must be MinguoEra");
 290         }
 291         return (era == MinguoEra.ROC ? yearOfEra : 1 - yearOfEra);
 292     }
 293 
 294     @Override
 295     public Era eraOf(int eraValue) {
 296         return MinguoEra.of(eraValue);
 297     }
 298 
 299     @Override
 300     public List<Era> eras() {
 301         return Arrays.<Era>asList(MinguoEra.values());
 302     }
 303 
 304     //-----------------------------------------------------------------------
 305     @Override
 306     public ValueRange range(ChronoField field) {
 307         switch (field) {
 308             case PROLEPTIC_MONTH: {
 309                 ValueRange range = PROLEPTIC_MONTH.range();
 310                 return ValueRange.of(range.getMinimum() - YEARS_DIFFERENCE * 12L, range.getMaximum() - YEARS_DIFFERENCE * 12L);
 311             }
 312             case YEAR_OF_ERA: {
 313                 ValueRange range = YEAR.range();
 314                 return ValueRange.of(1, range.getMaximum() - YEARS_DIFFERENCE, -range.getMinimum() + 1 + YEARS_DIFFERENCE);
 315             }
 316             case YEAR: {
 317                 ValueRange range = YEAR.range();
 318                 return ValueRange.of(range.getMinimum() - YEARS_DIFFERENCE, range.getMaximum() - YEARS_DIFFERENCE);
 319             }
 320         }
 321         return field.range();
 322     }
 323 
 324 }