Home | History | Annotate | Download | only in util
      1 /*
      2  *******************************************************************************
      3  * Copyright (C) 1996-2010, International Business Machines Corporation and    *
      4  * others. All Rights Reserved.                                                *
      5  *******************************************************************************
      6  */
      7 
      8 package com.ibm.icu.util;
      9 
     10 import java.util.Date;
     11 import java.util.Locale;
     12 
     13 /**
     14  * <code>TaiwanCalendar</code> is a subclass of <code>GregorianCalendar</code>
     15  * that numbers years since 1912.
     16  * <p>
     17  * The Taiwan calendar is identical to the Gregorian calendar in all respects
     18  * except for the year and era.  Years are numbered since 1912 AD (Gregorian).
     19  * <p>
     20  * The Taiwan Calendar has one era: <code>MINGUO</code>.
     21  * <p>
     22  * This class should not be subclassed.</p>
     23  * <p>
     24  * TaiwanCalendar usually should be instantiated using
     25  * {@link com.ibm.icu.util.Calendar#getInstance(ULocale)} passing in a <code>ULocale</code>
     26  * with the tag <code>"@calendar=roc"</code>.</p>
     27  *
     28  * @see com.ibm.icu.util.Calendar
     29  * @see com.ibm.icu.util.GregorianCalendar
     30  *
     31  * @author Laura Werner
     32  * @author Alan Liu
     33  * @author Steven R. Loomis
     34  * @stable ICU 3.8
     35  */
     36 public class TaiwanCalendar extends GregorianCalendar {
     37     // jdk1.4.2 serialver
     38     private static final long serialVersionUID = 2583005278132380631L;
     39 
     40     //-------------------------------------------------------------------------
     41     // Constructors...
     42     //-------------------------------------------------------------------------
     43 
     44     /**
     45      * Constant for the Taiwan Era for years before Minguo 1.
     46      * Brefore Minuo 1 is Gregorian 1911, Before Minguo 2 is Gregorian 1910
     47      * and so on.
     48      *
     49      * @see com.ibm.icu.util.Calendar#ERA
     50      * @stable ICU 3.8
     51      */
     52     public static final int BEFORE_MINGUO = 0;
     53 
     54     /**
     55      * Constant for the Taiwan Era for Minguo.  Minguo 1 is 1912 in
     56      * Gregorian calendar.
     57      *
     58      * @see com.ibm.icu.util.Calendar#ERA
     59      * @stable ICU 3.8
     60      */
     61     public static final int MINGUO = 1;
     62 
     63     /**
     64      * Constructs a <code>TaiwanCalendar</code> using the current time
     65      * in the default time zone with the default locale.
     66      * @stable ICU 3.8
     67      */
     68     public TaiwanCalendar() {
     69         super();
     70     }
     71 
     72     /**
     73      * Constructs a <code>TaiwanCalendar</code> based on the current time
     74      * in the given time zone with the default locale.
     75      *
     76      * @param zone the given time zone.
     77      * @stable ICU 3.8
     78      */
     79     public TaiwanCalendar(TimeZone zone) {
     80         super(zone);
     81     }
     82 
     83     /**
     84      * Constructs a <code>TaiwanCalendar</code> based on the current time
     85      * in the default time zone with the given locale.
     86      *
     87      * @param aLocale the given locale.
     88      * @stable ICU 3.8
     89      */
     90     public TaiwanCalendar(Locale aLocale) {
     91         super(aLocale);
     92     }
     93 
     94     /**
     95      * Constructs a <code>TaiwanCalendar</code> based on the current time
     96      * in the default time zone with the given locale.
     97      *
     98      * @param locale the given ulocale.
     99      * @stable ICU 3.8
    100      */
    101     public TaiwanCalendar(ULocale locale) {
    102         super(locale);
    103     }
    104 
    105     /**
    106      * Constructs a <code>TaiwanCalendar</code> based on the current time
    107      * in the given time zone with the given locale.
    108      *
    109      * @param zone the given time zone.
    110      *
    111      * @stable ICU 3.8
    112      */
    113     public TaiwanCalendar(TimeZone zone, Locale aLocale) {
    114         super(zone, aLocale);
    115     }
    116 
    117     /**
    118      * Constructs a <code>TaiwanCalendar</code> based on the current time
    119      * in the given time zone with the given locale.
    120      *
    121      * @param zone the given time zone.
    122      * @param locale the given ulocale.
    123      * @stable ICU 3.8
    124      */
    125     public TaiwanCalendar(TimeZone zone, ULocale locale) {
    126         super(zone, locale);
    127     }
    128 
    129     /**
    130      * Constructs a <code>TaiwanCalendar</code> with the given date set
    131      * in the default time zone with the default locale.
    132      *
    133      * @param date      The date to which the new calendar is set.
    134      * @stable ICU 3.8
    135      */
    136     public TaiwanCalendar(Date date) {
    137         this();
    138         setTime(date);
    139     }
    140 
    141     /**
    142      * Constructs a <code>TaiwanCalendar</code> with the given date set
    143      * in the default time zone with the default locale.
    144      *
    145      * @param year      The value used to set the calendar's {@link #YEAR YEAR} time field.
    146      *
    147      * @param month     The value used to set the calendar's {@link #MONTH MONTH} time field.
    148      *                  The value is 0-based. e.g., 0 for January.
    149      *
    150      * @param date      The value used to set the calendar's {@link #DATE DATE} time field.
    151      * @stable ICU 3.8
    152      */
    153     public TaiwanCalendar(int year, int month, int date) {
    154         super(year, month, date);
    155     }
    156 
    157     /**
    158      * Constructs a TaiwanCalendar with the given date
    159      * and time set for the default time zone with the default locale.
    160      *
    161      * @param year      The value used to set the calendar's {@link #YEAR YEAR} time field.
    162      *
    163      * @param month     The value used to set the calendar's {@link #MONTH MONTH} time field.
    164      *                  The value is 0-based. e.g., 0 for January.
    165      * @param date      The value used to set the calendar's {@link #DATE DATE} time field.
    166      * @param hour      The value used to set the calendar's {@link #HOUR_OF_DAY HOUR_OF_DAY} time field.
    167      * @param minute    The value used to set the calendar's {@link #MINUTE MINUTE} time field.
    168      * @param second    The value used to set the calendar's {@link #SECOND SECOND} time field.
    169      * @stable ICU 3.8
    170      */
    171     public TaiwanCalendar(int year, int month, int date, int hour,
    172                              int minute, int second)
    173     {
    174         super(year, month, date, hour, minute, second);
    175     }
    176 
    177 
    178     //-------------------------------------------------------------------------
    179     // The only practical difference from a Gregorian calendar is that years
    180     // are numbered since 1912, inclusive.  A couple of overrides will
    181     // take care of that....
    182     //-------------------------------------------------------------------------
    183 
    184     private static final int Taiwan_ERA_START = 1911; // 0=1911, 1=1912
    185 
    186     // Use 1970 as the default value of EXTENDED_YEAR
    187     private static final int GREGORIAN_EPOCH = 1970;
    188 
    189 
    190     /**
    191      * {@inheritDoc}
    192      * @stable ICU 3.8
    193      */
    194     protected int handleGetExtendedYear() {
    195         // EXTENDED_YEAR in TaiwanCalendar is a Gregorian year
    196         // The default value of EXTENDED_YEAR is 1970 (Minguo 59)
    197         int year = GREGORIAN_EPOCH;
    198         if (newerField(EXTENDED_YEAR, YEAR) == EXTENDED_YEAR
    199                 && newerField(EXTENDED_YEAR, ERA) == EXTENDED_YEAR) {
    200             year = internalGet(EXTENDED_YEAR, GREGORIAN_EPOCH);
    201         } else {
    202             int era = internalGet(ERA, MINGUO);
    203             if (era == MINGUO) {
    204                 year = internalGet(YEAR, 1) + Taiwan_ERA_START;
    205             } else {
    206                 year = 1 - internalGet(YEAR, 1) + Taiwan_ERA_START;
    207             }
    208         }
    209         return year;
    210     }
    211 
    212     /**
    213      * {@inheritDoc}
    214      * @stable ICU 3.8
    215      */
    216     protected void handleComputeFields(int julianDay) {
    217         super.handleComputeFields(julianDay);
    218         int y = internalGet(EXTENDED_YEAR) - Taiwan_ERA_START;
    219         if (y > 0) {
    220             internalSet(ERA, MINGUO);
    221             internalSet(YEAR, y);
    222         } else {
    223             internalSet(ERA, BEFORE_MINGUO);
    224             internalSet(YEAR, 1- y);
    225         }
    226     }
    227 
    228     /**
    229      * Override GregorianCalendar.  There is only one Taiwan ERA.  We
    230      * should really handle YEAR, YEAR_WOY, and EXTENDED_YEAR here too to
    231      * implement the 1..5000000 range, but it's not critical.
    232      * @stable ICU 3.8
    233      */
    234     protected int handleGetLimit(int field, int limitType) {
    235         if (field == ERA) {
    236             if (limitType == MINIMUM || limitType == GREATEST_MINIMUM) {
    237                 return BEFORE_MINGUO;
    238             } else {
    239                 return MINGUO;
    240             }
    241         }
    242         return super.handleGetLimit(field, limitType);
    243     }
    244 
    245     /**
    246      * {@inheritDoc}
    247      * @stable ICU 3.8
    248      */
    249     public String getType() {
    250         return "roc";
    251     }
    252 }
    253