Java : JapaneseEra with Examples
JapaneseEra (Java SE 21 & JDK 21) with Examples.
You will find code examples on most JapaneseEra methods.
Summary
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
final var era = JapaneseEra.HEISEI;
System.out.println(era); // Heisei
System.out.println(era.getValue()); // 2
static final JapaneseEra MEIJI
final var era = JapaneseEra.MEIJI;
System.out.println(era); // Meiji
System.out.println(era.getValue()); // -1
static final JapaneseEra REIWA
final var era = JapaneseEra.REIWA;
System.out.println(era); // Reiwa
System.out.println(era.getValue()); // 3
static final JapaneseEra SHOWA
final var era = JapaneseEra.SHOWA;
System.out.println(era); // Showa
System.out.println(era.getValue()); // 1
static final JapaneseEra TAISHO
final var era = JapaneseEra.TAISHO;
System.out.println(era); // Taisho
System.out.println(era.getValue()); // 0
Methods
String getDisplayName (TextStyle style, Locale locale)
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 ()
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)
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)
final var era = JapaneseEra.REIWA;
System.out.println(era); // Reiwa
System.out.println(era.range(ChronoField.ERA)); // -1 - 3
static JapaneseEra valueOf (String japaneseEra)
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 ()
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