src/share/classes/java/time/chrono/JapaneseEra.java
Print this page
@@ -59,10 +59,11 @@
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package java.time.chrono;
+import static java.time.chrono.JapaneseDate.MEIJI_6_ISODATE;
import static java.time.temporal.ChronoField.ERA;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
@@ -82,37 +83,29 @@
/**
* An era in the Japanese Imperial calendar system.
* <p>
* This class defines the valid eras for the Japanese chronology.
- * Only Meiji (1868-09-08 - 1912-07-29) and later eras are supported.
- * Japan introduced the Gregorian calendar since Meiji 6. The dates
- * between Meiji 1 - 5 are not historically correct.
- * The older eras are recognized as Seireki (Western calendar) era,
- * and the year of era of Seireki is proleptic Gregorian year.
- * (The Julian to Gregorian transition is not supported.)
+ * Japan introduced the Gregorian calendar starting with Meiji 6.
+ * Only Meiji and later eras are supported;
+ * dates before Meiji 6, January 1 are not supported.
*
* @implSpec
* This class is immutable and thread-safe.
*
* @since 1.8
*/
public final class JapaneseEra
implements Era, Serializable {
// The offset value to 0-based index from the era value.
- // i.e., getValue() + ERA_OFFSET == 0-based index; except that -999 is mapped to zero
+ // i.e., getValue() + ERA_OFFSET == 0-based index
static final int ERA_OFFSET = 2;
static final sun.util.calendar.Era[] ERA_CONFIG;
/**
- * The singleton instance for the before Meiji era ( - 1868-09-07)
- * which has the value -999.
- */
- public static final JapaneseEra SEIREKI = new JapaneseEra(-999, LocalDate.MIN);
- /**
* The singleton instance for the 'Meiji' era (1868-09-08 - 1912-07-29)
* which has the value -1.
*/
public static final JapaneseEra MEIJI = new JapaneseEra(-1, LocalDate.of(1868, 9, 8));
/**
@@ -142,21 +135,17 @@
// array for the singleton JapaneseEra instances
private static final JapaneseEra[] KNOWN_ERAS;
static {
- sun.util.calendar.Era[] sunEras = JapaneseChronology.JCAL.getEras();
- ERA_CONFIG = new sun.util.calendar.Era[sunEras.length + 1];
- for (int i = 1; i < ERA_CONFIG.length; i++) {
- ERA_CONFIG[i] = sunEras[i - 1];
- }
+ ERA_CONFIG = JapaneseChronology.JCAL.getEras();
+
KNOWN_ERAS = new JapaneseEra[ERA_CONFIG.length];
- KNOWN_ERAS[0] = SEIREKI;
- KNOWN_ERAS[1] = MEIJI;
- KNOWN_ERAS[2] = TAISHO;
- KNOWN_ERAS[3] = SHOWA;
- KNOWN_ERAS[4] = HEISEI;
+ KNOWN_ERAS[0] = MEIJI;
+ KNOWN_ERAS[1] = TAISHO;
+ KNOWN_ERAS[2] = SHOWA;
+ KNOWN_ERAS[3] = HEISEI;
for (int i = N_ERA_CONSTANTS; i < ERA_CONFIG.length; i++) {
CalendarDate date = ERA_CONFIG[i].getSinceDate();
LocalDate isoDate = LocalDate.of(date.getYear(), date.getMonth(), date.getDayOfMonth());
KNOWN_ERAS[i] = new JapaneseEra(i - ERA_OFFSET, isoDate);
}
@@ -201,14 +190,12 @@
}
//-----------------------------------------------------------------------
/**
* Returns the Sun private Era instance corresponding to this {@code JapaneseEra}.
- * SEIREKI doesn't have its corresponding one.
*
- * @return the Sun private Era instance for this {@code JapaneseEra},
- * or null for SEIREKI.
+ * @return the Sun private Era instance for this {@code JapaneseEra}.
*/
sun.util.calendar.Era getPrivateEra() {
return ERA_CONFIG[ordinal(eraValue)];
}
@@ -216,20 +203,18 @@
/**
* Obtains an instance of {@code JapaneseEra} from an {@code int} value.
* <p>
* The {@link #SHOWA} era that contains 1970-01-01 (ISO calendar system) has the value 1
* Later era is numbered 2 ({@link #HEISEI}). Earlier eras are numbered 0 ({@link #TAISHO}),
- * -1 ({@link #MEIJI}), only Meiji and later eras are supported. The prior to Meiji,
- * {@link #SEIREKI} is used.
+ * -1 ({@link #MEIJI}), only Meiji and later eras are supported.
*
* @param japaneseEra the era to represent
* @return the {@code JapaneseEra} singleton, not null
* @throws DateTimeException if the value is invalid
*/
public static JapaneseEra of(int japaneseEra) {
- if (japaneseEra != SEIREKI.eraValue &&
- (japaneseEra < MEIJI.eraValue || japaneseEra > HEISEI.eraValue)) {
+ if (japaneseEra < MEIJI.eraValue || japaneseEra > HEISEI.eraValue) {
throw new DateTimeException("Invalid era: " + japaneseEra);
}
return KNOWN_ERAS[ordinal(japaneseEra)];
}
@@ -274,26 +259,29 @@
*
* @param date the date, not null
* @return the Era singleton, never null
*/
static JapaneseEra from(LocalDate date) {
+ if (date.isBefore(MEIJI_6_ISODATE)) {
+ throw new DateTimeException("JapaneseDate before Meiji 6 are not supported");
+ }
for (int i = KNOWN_ERAS.length - 1; i > 0; i--) {
JapaneseEra era = KNOWN_ERAS[i];
if (date.compareTo(era.since) >= 0) {
return era;
}
}
- return SEIREKI;
+ return null;
}
static JapaneseEra toJapaneseEra(sun.util.calendar.Era privateEra) {
- for (int i = ERA_CONFIG.length - 1; i > 0; i--) {
+ for (int i = ERA_CONFIG.length - 1; i >= 0; i--) {
if (ERA_CONFIG[i].equals(privateEra)) {
return KNOWN_ERAS[i];
}
}
- return SEIREKI;
+ return null;
}
static sun.util.calendar.Era privateEraFrom(LocalDate isoDate) {
for (int i = KNOWN_ERAS.length - 1; i > 0; i--) {
JapaneseEra era = KNOWN_ERAS[i];
@@ -304,26 +292,26 @@
return null;
}
/**
* Returns the index into the arrays from the Era value.
- * the eraValue is a valid Era number, -999, -1..2.
+ * the eraValue is a valid Era number, -1..2.
*
* @param eraValue the era value to convert to the index
* @return the index of the current Era
*/
private static int ordinal(int eraValue) {
- return (eraValue == SEIREKI.eraValue) ? 0 : eraValue + ERA_OFFSET;
+ return eraValue + ERA_OFFSET - 1;
}
//-----------------------------------------------------------------------
/**
* Gets the numeric era {@code int} value.
* <p>
* The {@link #SHOWA} era that contains 1970-01-01 (ISO calendar system) has the value 1.
* Later eras are numbered from 2 ({@link #HEISEI}).
- * Earlier eras are numbered 0 ({@link #TAISHO}), -1 ({@link #MEIJI}), and -999 ({@link #SEIREKI}).
+ * Earlier eras are numbered 0 ({@link #TAISHO}), -1 ({@link #MEIJI})).
*
* @return the era value
*/
@Override
public int getValue() {
@@ -372,15 +360,11 @@
}
return ERA_CONFIG[index].getAbbreviation();
}
String getName() {
- int index = ordinal(getValue());
- if (index == 0) {
- return "Seireki";
- }
- return ERA_CONFIG[index].getName();
+ return ERA_CONFIG[ordinal(getValue())].getName();
}
@Override
public String toString() {
return getName();