Java : Era with Examples
Era (Java SE 21 & JDK 21) with Examples.
You will find code examples on most Era methods.
Summary
final var locale = Locale.getDefault();
System.out.println(locale.toLanguageTag()); // en-US
{
final Era era = IsoEra.CE;
System.out.println(era.getValue()); // 1
final var name = era.getDisplayName(TextStyle.FULL, locale);
System.out.println(name); // Anno Domini
}
{
final Era era = IsoEra.BCE;
System.out.println(era.getValue()); // 0
final var name = era.getDisplayName(TextStyle.FULL, locale);
System.out.println(name); // Before Christ
}
final var date1 = LocalDate.of(2100, 12, 31);
System.out.println(date1); // 2100-12-31
System.out.println(date1.getEra()); // CE
final var date2 = LocalDate.of(-99, 1, 2);
System.out.println(date2); // -0099-01-02
System.out.println(date2.getEra()); // BCE
Methods
default Temporal adjustInto (Temporal temporal)
final Era era = IsoEra.BCE;
System.out.println(era); // BCE
final var date = LocalDate.of(2100, 2, 3);
System.out.println(date); // 2100-02-03
final var ret = era.adjustInto(date);
System.out.println(ret); // -2099-02-03
default int get (TemporalField field)
final Era era = IsoEra.CE;
System.out.println(era); // CE
System.out.println(era.get(ChronoField.ERA)); // 1
final Era era = IsoEra.BCE;
System.out.println(era); // BCE
System.out.println(era.get(ChronoField.ERA)); // 0
default String getDisplayName (TextStyle style, Locale locale)
final Era era = IsoEra.CE;
System.out.println(era); // CE
final var name1 = era.getDisplayName(TextStyle.FULL, Locale.US);
System.out.println(name1); // Anno Domini
final var name2 = era.getDisplayName(TextStyle.FULL, Locale.JAPAN);
System.out.println(name2); // 西暦
final Era era = IsoEra.BCE;
System.out.println(era); // BCE
final var name1 = era.getDisplayName(TextStyle.FULL, Locale.US);
System.out.println(name1); // Before Christ
final var name2 = era.getDisplayName(TextStyle.FULL, Locale.JAPAN);
System.out.println(name2); // 紀元前
default long getLong (TemporalField field)
final Era era = IsoEra.CE;
System.out.println(era); // CE
System.out.println(era.getLong(ChronoField.ERA)); // 1
final Era era = IsoEra.BCE;
System.out.println(era); // BCE
System.out.println(era.getLong(ChronoField.ERA)); // 0
int getValue ()
final Era era = IsoEra.CE;
System.out.println(era); // CE
System.out.println(era.getValue()); // 1
final Era era = IsoEra.BCE;
System.out.println(era); // BCE
System.out.println(era.getValue()); // 0
default boolean isSupported (TemporalField field)
final Era era = IsoEra.CE;
System.out.println(era); // CE
System.out.println(era.isSupported(ChronoField.ERA)); // true
System.out.println(era.isSupported(ChronoField.YEAR)); // false
default <R> R query (TemporalQuery<R> query)
final Era era = IsoEra.CE;
System.out.println(era); // CE
final var ret = era.query(TemporalQueries.precision());
System.out.println(ret); // Eras
default ValueRange range (TemporalField field)
final Era era = IsoEra.CE;
System.out.println(era); // CE
System.out.println(era.range(ChronoField.ERA)); // 0 - 1
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