Java : Era with Examples

Era (Java SE 20 & JDK 20) with Examples.
You will find code examples on most Era methods.


Summary

An era of the time-line.

Class diagram

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)

Adjusts the specified temporal object to have the same era as this object.

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)

Gets the value of the specified field from this era as an int.

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)

Gets the textual representation of this era.

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)

Gets the value of the specified field from this era as a long.

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 ()

Gets the numeric value associated with the era as defined by the chronology.

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)

Checks if the specified field is supported.

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)

Queries this era using the specified 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)

Gets the range of valid values for the specified field.

final Era era = IsoEra.CE;
System.out.println(era); // CE

System.out.println(era.range(ChronoField.ERA)); // 0 - 1

Related posts

To top of page