Java : JapaneseEra with Examples
JapaneseEra (Java SE 21 & JDK 21) with Examples.
You will find code examples on most JapaneseEra methods.
Summary
An era in the Japanese Imperial calendar system.
final var locale = Locale.getDefault();
System.out.println(locale.toLanguageTag()); // en-US
{
final var era = JapaneseEra.SHOWA;
System.out.println(era.getValue()); // 1
final var name = era.getDisplayName(TextStyle.FULL, locale);
System.out.println(name); // Shōwa
}
{
final var era = JapaneseEra.HEISEI;
System.out.println(era.getValue()); // 2
final var name = era.getDisplayName(TextStyle.FULL, locale);
System.out.println(name); // Heisei
}
{
final var era = JapaneseEra.REIWA;
System.out.println(era.getValue()); // 3
final var name = era.getDisplayName(TextStyle.FULL, locale);
System.out.println(name); // Reiwa
}
final var japaneseDate1 = JapaneseDate.of(1989, 1, 8);
System.out.println(japaneseDate1); // Japanese Heisei 1-01-08
System.out.println(japaneseDate1.getEra()); // Heisei
final var japaneseDate2 = JapaneseDate.of(2019, 4, 30);
System.out.println(japaneseDate2); // Japanese Heisei 31-04-30
System.out.println(japaneseDate2.getEra()); // Heisei
final var japaneseDate3 = JapaneseDate.of(2019, 5, 1);
System.out.println(japaneseDate3); // Japanese Reiwa 1-05-01
System.out.println(japaneseDate3.getEra()); // Reiwa
Fields
static final JapaneseEra HEISEI
The singleton instance for the 'Heisei' era (1989-01-08 - 2019-04-30) which has the value 2.
final var era = JapaneseEra.HEISEI;
System.out.println(era); // Heisei
System.out.println(era.getValue()); // 2
static final JapaneseEra MEIJI
The singleton instance for the 'Meiji' era (1868-01-01 - 1912-07-29) which has the value -1.
final var era = JapaneseEra.MEIJI;
System.out.println(era); // Meiji
System.out.println(era.getValue()); // -1
static final JapaneseEra REIWA
The singleton instance for the 'Reiwa' era (2019-05-01 - ) which has the value 3.
final var era = JapaneseEra.REIWA;
System.out.println(era); // Reiwa
System.out.println(era.getValue()); // 3
static final JapaneseEra SHOWA
The singleton instance for the 'Showa' era (1926-12-25 - 1989-01-07) which has the value 1.
final var era = JapaneseEra.SHOWA;
System.out.println(era); // Showa
System.out.println(era.getValue()); // 1
static final JapaneseEra TAISHO
The singleton instance for the 'Taisho' era (1912-07-30 - 1926-12-24) which has the value 0.
final var era = JapaneseEra.TAISHO;
System.out.println(era); // Taisho
System.out.println(era.getValue()); // 0
Methods
String getDisplayName (TextStyle style, Locale locale)
Gets the textual representation of this era.
final var era = JapaneseEra.HEISEI;
System.out.println(era); // Heisei
final var name1 = era.getDisplayName(TextStyle.FULL, Locale.US);
System.out.println(name1); // Heisei
final var name2 = era.getDisplayName(TextStyle.FULL, Locale.JAPAN);
System.out.println(name2); // 平成
final var era = JapaneseEra.REIWA;
System.out.println(era); // Reiwa
final var name1 = era.getDisplayName(TextStyle.FULL, Locale.US);
System.out.println(name1); // Reiwa
final var name2 = era.getDisplayName(TextStyle.FULL, Locale.JAPAN);
System.out.println(name2); // 令和
int getValue ()
Gets the numeric era int value.
for (final var era : JapaneseEra.values()) {
System.out.printf("%s : value = %d%n", era, era.getValue());
}
// Result
// ↓
//Meiji : value = -1
//Taisho : value = 0
//Showa : value = 1
//Heisei : value = 2
//Reiwa : value = 3
static JapaneseEra of (int japaneseEra)
Obtains an instance of JapaneseEra from an int value.
final var era1 = JapaneseEra.of(2);
System.out.println(era1); // Heisei
final var era2 = JapaneseEra.of(3);
System.out.println(era2); // Reiwa
ValueRange range (TemporalField field)
Gets the range of valid values for the specified field.
final var era = JapaneseEra.REIWA;
System.out.println(era); // Reiwa
System.out.println(era.range(ChronoField.ERA)); // -1 - 3
static JapaneseEra valueOf (String japaneseEra)
Returns the JapaneseEra with the name.
final var era1 = JapaneseEra.valueOf("Heisei");
System.out.println(era1); // Heisei
final var era2 = JapaneseEra.valueOf("Reiwa");
System.out.println(era2); // Reiwa
static JapaneseEra[] values ()
Returns an array of JapaneseEras.
for (final var era : JapaneseEra.values()) {
System.out.printf("%s : value = %d%n", era, era.getValue());
}
// Result
// ↓
//Meiji : value = -1
//Taisho : value = 0
//Showa : value = 1
//Heisei : value = 2
//Reiwa : value = 3
Methods declared in Era
Related posts
- Get the current time
- Basics of Date and Time
- Don't use the legacy Date and Calendar classes, use new APIs instead
- API Examples
- Calendar
- ChronoLocalDate
- ChronoLocalDateTime
- ChronoZonedDateTime
- Clock
- Date
- DateTimeException
- DateTimeParseException
- DayOfWeek
- Duration
- Era
- Instant
- InstantSource
- JapaneseDate
- LocalDate
- LocalDateTime
- LocalTime
- Month
- MonthDay
- OffsetDateTime
- OffsetTime
- Period
- Temporal
- TemporalAccessor
- TemporalAdjuster
- TemporalAdjusters
- TimeZone
- Year
- YearMonth
- ZonedDateTime
- ZoneId
- ZoneOffset