Interface Era

All Superinterfaces:
TemporalAccessor, TemporalAdjuster
All Known Implementing Classes:
HijrahEra, IsoEra, JapaneseEra, MinguoEra, ThaiBuddhistEra

public interface Era extends TemporalAccessor, TemporalAdjuster
An era of the time-line.

Most calendar systems have a single epoch dividing the time-line into two eras. However, some calendar systems, have multiple eras, such as one for the reign of each leader. In all cases, the era is conceptually the largest division of the time-line. Each chronology defines the Era's that are known Eras and a Chrono.eras to get the valid eras.

For example, the Thai Buddhist calendar system divides time into two eras, before and after a single date. By contrast, the Japanese calendar system has one era for the reign of each Emperor.

Instances of Era may be compared using the == operator.

Specification for implementors

This interface must be implemented with care to ensure other classes operate correctly. All implementations must be singletons - final, immutable and thread-safe. It is recommended to use an enum whenever possible.
  • Method Details

    • isSupported

      default boolean isSupported(TemporalField field)
      Description copied from interface: TemporalAccessor
      Checks if the specified field is supported.

      This checks if the date-time can be queried for the specified field. If false, then calling the range and get methods will throw an exception.

      Specification for implementors

      Implementations must check and handle all fields defined in ChronoField. If the field is supported, then true is returned, otherwise false

      If the field is not a ChronoField, then the result of this method is obtained by invoking TemporalField.isSupportedBy(TemporalAccessor) passing this as the argument.

      Implementations must not alter this object.

      Specified by:
      isSupported in interface TemporalAccessor
      Parameters:
      field - the field to check, null returns false
      Returns:
      true if this date-time can be queried for the field, false if not
    • get

      default int get(TemporalField field)
      Description copied from interface: TemporalAccessor
      Gets the value of the specified field as an int.

      This queries the date-time for the value for the specified field. The returned value will always be within the valid range of values for the field. If the date-time cannot return the value, because the field is unsupported or for some other reason, an exception will be thrown.

      Specification for implementors

      Implementations must check and handle all fields defined in ChronoField. If the field is supported and has an int range, then the value of the field must be returned. If unsupported, then a DateTimeException must be thrown.

      If the field is not a ChronoField, then the result of this method is obtained by invoking TemporalField.getFrom(TemporalAccessor) passing this as the argument.

      Implementations must not alter either this object.

      Specified by:
      get in interface TemporalAccessor
      Parameters:
      field - the field to get, not null
      Returns:
      the value for the field, within the valid range of values
    • getLong

      default long getLong(TemporalField field)
      Description copied from interface: TemporalAccessor
      Gets the value of the specified field as a long.

      This queries the date-time for the value for the specified field. The returned value may be outside the valid range of values for the field. If the date-time cannot return the value, because the field is unsupported or for some other reason, an exception will be thrown.

      Specification for implementors

      Implementations must check and handle all fields defined in ChronoField. If the field is supported, then the value of the field must be returned. If unsupported, then a DateTimeException must be thrown.

      If the field is not a ChronoField, then the result of this method is obtained by invoking TemporalField.getFrom(TemporalAccessor) passing this as the argument.

      Implementations must not alter either this object.

      Specified by:
      getLong in interface TemporalAccessor
      Parameters:
      field - the field to get, not null
      Returns:
      the value for the field
    • query

      default <R> R query(TemporalQuery<R> query)
      Description copied from interface: TemporalAccessor
      Queries this date-time.

      This queries this date-time using the specified query strategy object.

      Queries are a key tool for extracting information from date-times. They exists to externalize the process of querying, permitting different approaches, as per the strategy design pattern. Examples might be a query that checks if the date is the day before February 29th in a leap year, or calculates the number of days to your next birthday.

      The most common query implementations are method references, such as LocalDate::from and ZoneId::from. Further implementations are on TemporalQueries. Queries may also be defined by applications.

      Specification for implementors

      Implementations of this method must behave as follows:
         public <R> R query(TemporalQuery<R> type) {
           // only include an if statement if the implementation can return it
           if (query == TemporalQueries.zoneId())  return // the ZoneId
           if (query == TemporalQueries.chronology())  return // the Chrono
           if (query == TemporalQueries.precision())  return // the precision
           // call default method
           return super.query(query);
         }
       
      Specified by:
      query in interface TemporalAccessor
      Type Parameters:
      R - the type of the result
      Parameters:
      query - the query to invoke, not null
      Returns:
      the query result, null may be returned (defined by the query)
    • adjustInto

      default Temporal adjustInto(Temporal temporal)
      Description copied from interface: TemporalAdjuster
      Adjusts the specified temporal object.

      This adjusts the specified temporal object using the logic encapsulated in the implementing class. Examples might be an adjuster that sets the date avoiding weekends, or one that sets the date to the last day of the month.

      There are two equivalent ways of using this method. The first is to invoke this method directly. The second is to use Temporal.with(TemporalAdjuster):

         // these two lines are equivalent, but the second approach is recommended
         temporal = thisAdjuster.adjustInto(temporal);
         temporal = temporal.with(thisAdjuster);
       
      It is recommended to use the second approach, with(TemporalAdjuster), as it is a lot clearer to read in code.

      Specification for implementors

      The implementation must take the input object and adjust it. The implementation defines the logic of the adjustment and is responsible for documenting that logic. It may use any method on Temporal to query the temporal object and perform the adjustment. The returned object must have the same observable type as the input object

      The input object must not be altered. Instead, an adjusted copy of the original must be returned. This provides equivalent, safe behavior for immutable and mutable temporal objects.

      The input temporal object may be in a calendar system other than ISO. Implementations may choose to document compatibility with other calendar systems, or reject non-ISO temporal objects by querying the chronology.

      This method may be called from multiple threads in parallel. It must be thread-safe when invoked.

      Specified by:
      adjustInto in interface TemporalAdjuster
      Parameters:
      temporal - the temporal object to adjust, not null
      Returns:
      an object of the same observable type with the adjustment made, not null
    • getValue

      int getValue()
      Gets the numeric value associated with the era as defined by the chronology. Each chronology defines the predefined Eras and methods to list the Eras of the chronology.

      All fields, including eras, have an associated numeric value. The meaning of the numeric value for era is determined by the chronology according to these principles:

      • The era in use at the epoch 1970-01-01 (ISO) has the value 1.
      • Later eras have sequentially higher values.
      • Earlier eras have sequentially lower values, which may be negative.

      Returns:
      the numeric era value
    • getDisplayName

      default String getDisplayName(TextStyle style, Locale locale)
      Gets the textual representation of this era.

      This returns the textual name used to identify the era. The parameters control the style of the returned text and the locale.

      If no textual mapping is found then the numeric value is returned.

      Parameters:
      style - the style of the text required, not null
      locale - the locale to use, not null
      Returns:
      the text value of the era, not null