37 * * Redistributions in binary form must reproduce the above copyright notice,
38 * this list of conditions and the following disclaimer in the documentation
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.calendar;
58
59 import java.io.DataInput;
60 import java.io.DataOutput;
61 import java.io.IOException;
62 import java.io.InvalidObjectException;
63 import java.io.ObjectStreamException;
64 import java.io.Serializable;
65 import java.time.DateTimeException;
66 import java.time.LocalDate;
67 import java.time.temporal.Era;
68 import java.util.Arrays;
69
70 import sun.util.calendar.CalendarDate;
71
72 /**
73 * An era in the Japanese Imperial calendar system.
74 * <p>
75 * This class defines the valid eras for the Japanese chronology.
76 * Only Meiji (1868-09-08 - 1912-07-29) and later eras are supported.
77 * Japan introduced the Gregorian calendar since Meiji 6. The dates
78 * between Meiji 1 - 5 are not historically correct.
79 * The older eras are recognized as Seireki (Western calendar) era,
80 * and the year of era of Seireki is proleptic Gregorian year.
81 * (The Julian to Gregorian transition is not supported.)
82 *
83 * <h3>Specification for implementors</h3>
84 * This class is immutable and thread-safe.
85 *
86 * @since 1.8
87 */
88 final class JapaneseEra
89 implements Era<JapaneseChrono>, Serializable {
90
91 // The offset value to 0-based index from the era value.
92 // i.e., getValue() + ERA_OFFSET == 0-based index; except that -999 is mapped to zero
93 static final int ERA_OFFSET = 2;
94
95 static final sun.util.calendar.Era[] ERA_CONFIG;
96
97 /**
98 * The singleton instance for the before Meiji era ( - 1868-09-07)
99 * which has the value -999.
100 */
101 public static final JapaneseEra SEIREKI = new JapaneseEra(-999, LocalDate.MIN);
102 /**
103 * The singleton instance for the 'Meiji' era (1868-09-08 - 1912-07-29)
104 * which has the value -1.
105 */
106 public static final JapaneseEra MEIJI = new JapaneseEra(-1, LocalDate.of(1868, 9, 8));
107 /**
108 * The singleton instance for the 'Taisho' era (1912-07-30 - 1926-12-24)
109 * which has the value 0.
116 public static final JapaneseEra SHOWA = new JapaneseEra(1, LocalDate.of(1926, 12, 25));
117 /**
118 * The singleton instance for the 'Heisei' era (1989-01-08 - current)
119 * which has the value 2.
120 */
121 public static final JapaneseEra HEISEI = new JapaneseEra(2, LocalDate.of(1989, 1, 8));
122
123 // the number of defined JapaneseEra constants.
124 // There could be an extra era defined in its configuration.
125 private static final int N_ERA_CONSTANTS = HEISEI.getValue() + ERA_OFFSET + 1;
126
127 /**
128 * Serialization version.
129 */
130 private static final long serialVersionUID = 1466499369062886794L;
131
132 // array for the singleton JapaneseEra instances
133 private static final JapaneseEra[] KNOWN_ERAS;
134
135 static {
136 sun.util.calendar.Era[] sunEras = JapaneseChrono.JCAL.getEras();
137 ERA_CONFIG = new sun.util.calendar.Era[sunEras.length + 1];
138 for (int i = 1; i < ERA_CONFIG.length; i++) {
139 ERA_CONFIG[i] = sunEras[i - 1];
140 }
141 KNOWN_ERAS = new JapaneseEra[ERA_CONFIG.length];
142 KNOWN_ERAS[0] = SEIREKI;
143 KNOWN_ERAS[1] = MEIJI;
144 KNOWN_ERAS[2] = TAISHO;
145 KNOWN_ERAS[3] = SHOWA;
146 KNOWN_ERAS[4] = HEISEI;
147 for (int i = N_ERA_CONSTANTS; i < ERA_CONFIG.length; i++) {
148 CalendarDate date = ERA_CONFIG[i].getSinceDate();
149 LocalDate isoDate = LocalDate.of(date.getYear(), date.getMonth(), date.getDayOfMonth());
150 KNOWN_ERAS[i] = new JapaneseEra(i - ERA_OFFSET, isoDate);
151 }
152 };
153
154 /**
155 * The era value.
156 * @serial
275 private static int ordinal(int eravalue) {
276 return (eravalue == SEIREKI.eraValue) ? 0 : eravalue + ERA_OFFSET;
277 }
278
279 //-----------------------------------------------------------------------
280 /**
281 * Returns the numeric value of this {@code JapaneseEra}.
282 * <p>
283 * The {@link #SHOWA} era that contains 1970-01-01 (ISO calendar system) has the value 1.
284 * Later eras are numbered from 2 ({@link #HEISEI}).
285 * Earlier eras are numbered 0 ({@link #TAISHO}), -1 ({@link #MEIJI}), and -999 ({@link #SEIREKI}).
286 *
287 * @return the era value
288 */
289 @Override
290 public int getValue() {
291 return eraValue;
292 }
293
294 @Override
295 public JapaneseChrono getChrono() {
296 return JapaneseChrono.INSTANCE;
297 }
298
299 //-----------------------------------------------------------------------
300 String getAbbreviation() {
301 int index = ordinal(getValue());
302 if (index == 0) {
303 return "";
304 }
305 return ERA_CONFIG[index].getAbbreviation();
306 }
307
308 String getName() {
309 int index = ordinal(getValue());
310 if (index == 0) {
311 return "Seireki";
312 }
313 return ERA_CONFIG[index].getName();
314 }
315
316 @Override
|
37 * * Redistributions in binary form must reproduce the above copyright notice,
38 * this list of conditions and the following disclaimer in the documentation
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 java.io.DataInput;
60 import java.io.DataOutput;
61 import java.io.IOException;
62 import java.io.InvalidObjectException;
63 import java.io.ObjectStreamException;
64 import java.io.Serializable;
65 import java.time.DateTimeException;
66 import java.time.LocalDate;
67 import java.util.Arrays;
68
69 import sun.util.calendar.CalendarDate;
70
71 /**
72 * An era in the Japanese Imperial calendar system.
73 * <p>
74 * This class defines the valid eras for the Japanese chronology.
75 * Only Meiji (1868-09-08 - 1912-07-29) and later eras are supported.
76 * Japan introduced the Gregorian calendar since Meiji 6. The dates
77 * between Meiji 1 - 5 are not historically correct.
78 * The older eras are recognized as Seireki (Western calendar) era,
79 * and the year of era of Seireki is proleptic Gregorian year.
80 * (The Julian to Gregorian transition is not supported.)
81 *
82 * <h3>Specification for implementors</h3>
83 * This class is immutable and thread-safe.
84 *
85 * @since 1.8
86 */
87 final class JapaneseEra
88 implements Era, Serializable {
89
90 // The offset value to 0-based index from the era value.
91 // i.e., getValue() + ERA_OFFSET == 0-based index; except that -999 is mapped to zero
92 static final int ERA_OFFSET = 2;
93
94 static final sun.util.calendar.Era[] ERA_CONFIG;
95
96 /**
97 * The singleton instance for the before Meiji era ( - 1868-09-07)
98 * which has the value -999.
99 */
100 public static final JapaneseEra SEIREKI = new JapaneseEra(-999, LocalDate.MIN);
101 /**
102 * The singleton instance for the 'Meiji' era (1868-09-08 - 1912-07-29)
103 * which has the value -1.
104 */
105 public static final JapaneseEra MEIJI = new JapaneseEra(-1, LocalDate.of(1868, 9, 8));
106 /**
107 * The singleton instance for the 'Taisho' era (1912-07-30 - 1926-12-24)
108 * which has the value 0.
115 public static final JapaneseEra SHOWA = new JapaneseEra(1, LocalDate.of(1926, 12, 25));
116 /**
117 * The singleton instance for the 'Heisei' era (1989-01-08 - current)
118 * which has the value 2.
119 */
120 public static final JapaneseEra HEISEI = new JapaneseEra(2, LocalDate.of(1989, 1, 8));
121
122 // the number of defined JapaneseEra constants.
123 // There could be an extra era defined in its configuration.
124 private static final int N_ERA_CONSTANTS = HEISEI.getValue() + ERA_OFFSET + 1;
125
126 /**
127 * Serialization version.
128 */
129 private static final long serialVersionUID = 1466499369062886794L;
130
131 // array for the singleton JapaneseEra instances
132 private static final JapaneseEra[] KNOWN_ERAS;
133
134 static {
135 sun.util.calendar.Era[] sunEras = JapaneseChronology.JCAL.getEras();
136 ERA_CONFIG = new sun.util.calendar.Era[sunEras.length + 1];
137 for (int i = 1; i < ERA_CONFIG.length; i++) {
138 ERA_CONFIG[i] = sunEras[i - 1];
139 }
140 KNOWN_ERAS = new JapaneseEra[ERA_CONFIG.length];
141 KNOWN_ERAS[0] = SEIREKI;
142 KNOWN_ERAS[1] = MEIJI;
143 KNOWN_ERAS[2] = TAISHO;
144 KNOWN_ERAS[3] = SHOWA;
145 KNOWN_ERAS[4] = HEISEI;
146 for (int i = N_ERA_CONSTANTS; i < ERA_CONFIG.length; i++) {
147 CalendarDate date = ERA_CONFIG[i].getSinceDate();
148 LocalDate isoDate = LocalDate.of(date.getYear(), date.getMonth(), date.getDayOfMonth());
149 KNOWN_ERAS[i] = new JapaneseEra(i - ERA_OFFSET, isoDate);
150 }
151 };
152
153 /**
154 * The era value.
155 * @serial
274 private static int ordinal(int eravalue) {
275 return (eravalue == SEIREKI.eraValue) ? 0 : eravalue + ERA_OFFSET;
276 }
277
278 //-----------------------------------------------------------------------
279 /**
280 * Returns the numeric value of this {@code JapaneseEra}.
281 * <p>
282 * The {@link #SHOWA} era that contains 1970-01-01 (ISO calendar system) has the value 1.
283 * Later eras are numbered from 2 ({@link #HEISEI}).
284 * Earlier eras are numbered 0 ({@link #TAISHO}), -1 ({@link #MEIJI}), and -999 ({@link #SEIREKI}).
285 *
286 * @return the era value
287 */
288 @Override
289 public int getValue() {
290 return eraValue;
291 }
292
293 @Override
294 public JapaneseChronology getChronology() {
295 return JapaneseChronology.INSTANCE;
296 }
297
298 //-----------------------------------------------------------------------
299 String getAbbreviation() {
300 int index = ordinal(getValue());
301 if (index == 0) {
302 return "";
303 }
304 return ERA_CONFIG[index].getAbbreviation();
305 }
306
307 String getName() {
308 int index = ordinal(getValue());
309 if (index == 0) {
310 return "Seireki";
311 }
312 return ERA_CONFIG[index].getName();
313 }
314
315 @Override
|