< prev index next >

src/java.base/share/classes/java/time/chrono/JapaneseEra.java

Print this page
rev 53194 : [mq]: 8217892


  71 import java.io.ObjectInputStream;
  72 import java.io.ObjectStreamException;
  73 import java.io.Serializable;
  74 import java.time.DateTimeException;
  75 import java.time.LocalDate;
  76 import java.time.format.DateTimeFormatterBuilder;
  77 import java.time.format.TextStyle;
  78 import java.time.temporal.ChronoField;
  79 import java.time.temporal.TemporalField;
  80 import java.time.temporal.UnsupportedTemporalTypeException;
  81 import java.time.temporal.ValueRange;
  82 import java.util.Arrays;
  83 import java.util.Locale;
  84 import java.util.Objects;
  85 
  86 import sun.util.calendar.CalendarDate;
  87 
  88 /**
  89  * An era in the Japanese Imperial calendar system.
  90  * <p>
  91  * This class defines the valid eras for the Japanese chronology.
  92  * Japan introduced the Gregorian calendar starting with Meiji 6.
  93  * Only Meiji and later eras are supported;
  94  * dates before Meiji 6, January 1 are not supported.
  95  * The number of the valid eras may increase, as new eras may be
  96  * defined by the Japanese government. Once an era is defined,
  97  * future versions of the platform may add a singleton instance
  98  * for it. The defined era is expected to have a consecutive integer
  99  * associated with it.


















 100  *
 101  * @implSpec
 102  * This class is immutable and thread-safe.
 103  *
 104  * @since 1.8
 105  */
 106 public final class JapaneseEra
 107         implements Era, Serializable {
 108 
 109     // The offset value to 0-based index from the era value.
 110     // i.e., getValue() + ERA_OFFSET == 0-based index
 111     static final int ERA_OFFSET = 2;
 112 
 113     static final sun.util.calendar.Era[] ERA_CONFIG;
 114 
 115     /**
 116      * The singleton instance for the 'Meiji' era (1868-01-01 - 1912-07-29)
 117      * which has the value -1.
 118      */
 119     public static final JapaneseEra MEIJI = new JapaneseEra(-1, LocalDate.of(1868, 1, 1));


 182      * @param since  the date representing the first date of the era, validated not null
 183      */
 184     private JapaneseEra(int eraValue, LocalDate since) {
 185         this.eraValue = eraValue;
 186         this.since = since;
 187     }
 188 
 189     //-----------------------------------------------------------------------
 190     /**
 191      * Returns the Sun private Era instance corresponding to this {@code JapaneseEra}.
 192      *
 193      * @return the Sun private Era instance for this {@code JapaneseEra}.
 194      */
 195     sun.util.calendar.Era getPrivateEra() {
 196         return ERA_CONFIG[ordinal(eraValue)];
 197     }
 198 
 199     //-----------------------------------------------------------------------
 200     /**
 201      * Obtains an instance of {@code JapaneseEra} from an {@code int} value.








 202      * <p>
 203      * The {@link #SHOWA} era that contains 1970-01-01 (ISO calendar system) has the value 1.
 204      * Later era is numbered 2 ({@link #HEISEI}). Earlier eras are numbered 0 ({@link #TAISHO}),
 205      * -1 ({@link #MEIJI}), only Meiji and later eras are supported.
 206      * <p>
 207      * In addition to the known era singletons, values for additional
 208      * eras may be defined. Those values are the {@link Era#getValue()}
 209      * of corresponding eras from the {@link #values()} method.
 210      *
 211      * @param japaneseEra  the era to represent
 212      * @return the {@code JapaneseEra} singleton, not null
 213      * @throws DateTimeException if the value is invalid
 214      */
 215     public static JapaneseEra of(int japaneseEra) {
 216         int i = ordinal(japaneseEra);
 217         if (i < 0 || i >= KNOWN_ERAS.length) {
 218             throw new DateTimeException("Invalid era: " + japaneseEra);
 219         }
 220         return KNOWN_ERAS[i];
 221     }
 222 
 223     /**
 224      * Returns the {@code JapaneseEra} with the name.
 225      * <p>
 226      * The string must match exactly the name of the era.
 227      * (Extraneous whitespace characters are not permitted.)
 228      * <p>
 229      * Valid era names are the names of eras returned from {@link #values()}.




  71 import java.io.ObjectInputStream;
  72 import java.io.ObjectStreamException;
  73 import java.io.Serializable;
  74 import java.time.DateTimeException;
  75 import java.time.LocalDate;
  76 import java.time.format.DateTimeFormatterBuilder;
  77 import java.time.format.TextStyle;
  78 import java.time.temporal.ChronoField;
  79 import java.time.temporal.TemporalField;
  80 import java.time.temporal.UnsupportedTemporalTypeException;
  81 import java.time.temporal.ValueRange;
  82 import java.util.Arrays;
  83 import java.util.Locale;
  84 import java.util.Objects;
  85 
  86 import sun.util.calendar.CalendarDate;
  87 
  88 /**
  89  * An era in the Japanese Imperial calendar system.
  90  * <p>
  91  * The Japanese government defines the official name and start date of
  92  * each era. Eras are consecutive and their date ranges do not overlap,
  93  * so the end date of one era is always the day before the start date
  94  * of the next era.
  95  * <p>
  96  * The Java SE Platform supports all eras defined by the Japanese government,
  97  * beginning with the Meiji era. Each era is identified in the Platform by an
  98  * integer value and a name. The {@link #of(int)} and {@link #valueOf(String)}
  99  * methods may be used to obtain a singleton instance of {@code JapaneseEra}
 100  * for each era. The {@link #values()} method returns the singleton instances
 101  * of all supported eras.
 102  * <p>
 103  * For convenience, this class declares a number of public static final fields
 104  * that refer to singleton instances returned by the {@link #values()} method.
 105  *
 106  * @apiNote
 107  * The fields declared in this class may evolve over time, in line with the
 108  * results of the {@link #values()} method. However, there is not necessarily
 109  * a 1:1 correspondence between the fields and the singleton instances.
 110  *
 111  * @apiNote
 112  * The Japanese government may announce a new era and define its start
 113  * date but not its official name. In this scenario, the singleton instance
 114  * that represents the new era may return a name that is not stable until
 115  * the official name is defined. Developers should exercise caution when
 116  * relying on the name returned by any singleton instance that does not
 117  * correspond to a public static final field.
 118  *
 119  * @implSpec
 120  * This class is immutable and thread-safe.
 121  *
 122  * @since 1.8
 123  */
 124 public final class JapaneseEra
 125         implements Era, Serializable {
 126 
 127     // The offset value to 0-based index from the era value.
 128     // i.e., getValue() + ERA_OFFSET == 0-based index
 129     static final int ERA_OFFSET = 2;
 130 
 131     static final sun.util.calendar.Era[] ERA_CONFIG;
 132 
 133     /**
 134      * The singleton instance for the 'Meiji' era (1868-01-01 - 1912-07-29)
 135      * which has the value -1.
 136      */
 137     public static final JapaneseEra MEIJI = new JapaneseEra(-1, LocalDate.of(1868, 1, 1));


 200      * @param since  the date representing the first date of the era, validated not null
 201      */
 202     private JapaneseEra(int eraValue, LocalDate since) {
 203         this.eraValue = eraValue;
 204         this.since = since;
 205     }
 206 
 207     //-----------------------------------------------------------------------
 208     /**
 209      * Returns the Sun private Era instance corresponding to this {@code JapaneseEra}.
 210      *
 211      * @return the Sun private Era instance for this {@code JapaneseEra}.
 212      */
 213     sun.util.calendar.Era getPrivateEra() {
 214         return ERA_CONFIG[ordinal(eraValue)];
 215     }
 216 
 217     //-----------------------------------------------------------------------
 218     /**
 219      * Obtains an instance of {@code JapaneseEra} from an {@code int} value.
 220      * <ul>
 221      * <li>The value {@code 1} is associated with the 'Showa' era, because
 222      * it contains 1970-01-01 (ISO calendar system).</li>
 223      * <li>The values {@code -1} and {@code 0} are associated with two earlier
 224      * eras, Meiji and Taisho, respectively.</li>
 225      * <li>A value greater than {@code 1} is associated with a later era,
 226      * beginning with Heisei ({@code 2}).</li>
 227      * </ul>
 228      * <p>
 229      * Every instance of {@code JapaneseEra} that is returned from the {@link #values()}
 230      * method has an int value (available via {@link Era#getValue()} which is
 231      * accepted by this method.




 232      *
 233      * @param japaneseEra  the era to represent
 234      * @return the {@code JapaneseEra} singleton, not null
 235      * @throws DateTimeException if the value is invalid
 236      */
 237     public static JapaneseEra of(int japaneseEra) {
 238         int i = ordinal(japaneseEra);
 239         if (i < 0 || i >= KNOWN_ERAS.length) {
 240             throw new DateTimeException("Invalid era: " + japaneseEra);
 241         }
 242         return KNOWN_ERAS[i];
 243     }
 244 
 245     /**
 246      * Returns the {@code JapaneseEra} with the name.
 247      * <p>
 248      * The string must match exactly the name of the era.
 249      * (Extraneous whitespace characters are not permitted.)
 250      * <p>
 251      * Valid era names are the names of eras returned from {@link #values()}.


< prev index next >