DateFormat.Field (Java SE 26 & JDK 26)
Description: declaration: module: java.base, package: java.text, class: DateFormat, class: Field
Keywords: java.text.DateFormat.Field class

# Class DateFormat.Field

java.lang.Object
java.text.AttributedCharacterIterator.Attribute
java.text.Format.Field
java.text.DateFormat.Field

All Implemented Interfaces:
  `Serializable`

Enclosing class:
  `DateFormat`

public static class DateFormat.Field extends Format.Field
Defines constants that are used as attribute keys in the `AttributedCharacterIterator` returned from `DateFormat.formatToCharacterIterator` and as field identifiers in `FieldPosition`.

The class also provides two methods to map between its constants and the corresponding Calendar constants.

Since:
  1.4
See Also:
  - `Calendar`
  - Serialized Form

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final DateFormat.Field`
  `AM_PM`
  Constant identifying the time of day indicator (e.g. "a.m." or "p.m.") field.
  `static final DateFormat.Field`
  `DAY_OF_MONTH`
  Constant identifying the day of month field.
  `static final DateFormat.Field`
  `DAY_OF_WEEK`
  Constant identifying the day of week field.
  `static final DateFormat.Field`
  `DAY_OF_WEEK_IN_MONTH`
  Constant identifying the day of week field.
  `static final DateFormat.Field`
  `DAY_OF_YEAR`
  Constant identifying the day of year field.
  `static final DateFormat.Field`
  `ERA`
  Constant identifying the era field.
  `static final DateFormat.Field`
  `HOUR_OF_DAY0`
  Constant identifying the hour of day field, where the legal values are 0 to 23.
  `static final DateFormat.Field`
  `HOUR_OF_DAY1`
  Constant identifying the hour of day field, where the legal values are 1 to 24.
  `static final DateFormat.Field`
  `HOUR0`
  Constant identifying the hour field, where the legal values are 0 to 11.
  `static final DateFormat.Field`
  `HOUR1`
  Constant identifying the hour field, where the legal values are 1 to 12.
  `static final DateFormat.Field`
  `MILLISECOND`
  Constant identifying the millisecond field.
  `static final DateFormat.Field`
  `MINUTE`
  Constant identifying the minute field.
  `static final DateFormat.Field`
  `MONTH`
  Constant identifying the month field.
  `static final DateFormat.Field`
  `SECOND`
  Constant identifying the second field.
  `static final DateFormat.Field`
  `TIME_ZONE`
  Constant identifying the time zone field.
  `static final DateFormat.Field`
  `WEEK_OF_MONTH`
  Constant identifying the week of month field.
  `static final DateFormat.Field`
  `WEEK_OF_YEAR`
  Constant identifying the week of year field.
  `static final DateFormat.Field`
  `YEAR`
  Constant identifying the year field.

  ### Fields declared in class AttributedCharacterIterator.Attribute

  `INPUT_METHOD_SEGMENT, LANGUAGE, READING`
  Modifier and Type
  Field
  Description
  `static final AttributedCharacterIterator.Attribute`
  `INPUT_METHOD_SEGMENT`
  Attribute key for input method segments.
  `static final AttributedCharacterIterator.Attribute`
  `LANGUAGE`
  Attribute key for the language of some text.
  `static final AttributedCharacterIterator.Attribute`
  `READING`
  Attribute key for the reading of some text.

- ## Constructor Summary

  Constructors
  Modifier
  Constructor
  Description
  `protected`
  `Field(String name, int calendarField)`
  Creates a `Field`.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `int`
  `getCalendarField()`
  Returns the `Calendar` field associated with this attribute.
  `static DateFormat.Field`
  `ofCalendarField(int calendarField)`
  Returns the `Field` constant that corresponds to the `Calendar` constant `calendarField`.
  `protected Object`
  `readResolve()`
  Resolves instances being deserialized to the predefined constants.

  ### Methods declared in class AttributedCharacterIterator.Attribute

  `equals, getName, hashCode, toString`
  Modifier and Type
  Method
  Description
  `final boolean`
  `equals(Object obj)`
  Compares two objects for equality.
  `protected String`
  `getName()`
  Returns the name of the attribute.
  `final int`
  `hashCode()`
  Returns a hash code value for the object.
  `String`
  `toString()`
  Returns a string representation of the object.

  ### Methods declared in class Object

  `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`
  Modifier and Type
  Method
  Description
  `protected Object`
  `clone()`
  Creates and returns a copy of this object.
  `protected void`
  `finalize()`
  Deprecated, for removal: This API element is subject to removal in a future version.
  Finalization is deprecated and subject to removal in a future release.
  `final Class<?>`
  `getClass()`
  Returns the runtime class of this `Object`.
  `final void`
  `notify()`
  Wakes up a single thread that is waiting on this object's monitor.
  `final void`
  `notifyAll()`
  Wakes up all threads that are waiting on this object's monitor.
  `final void`
  `wait()`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted.
  `final void`
  `wait(long timeoutMillis)`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.
  `final void`
  `wait(long timeoutMillis, int nanos)`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

- ## Field Details

  - ### ERA

    public static final DateFormat.Field ERA
    Constant identifying the era field.

  - ### YEAR

    public static final DateFormat.Field YEAR
    Constant identifying the year field.

  - ### MONTH

    public static final DateFormat.Field MONTH
    Constant identifying the month field.

  - ### DAY_OF_MONTH

    public static final DateFormat.Field DAY_OF_MONTH
    Constant identifying the day of month field.

  - ### HOUR_OF_DAY1

    public static final DateFormat.Field HOUR_OF_DAY1
    Constant identifying the hour of day field, where the legal values are 1 to 24.

  - ### HOUR_OF_DAY0

    public static final DateFormat.Field HOUR_OF_DAY0
    Constant identifying the hour of day field, where the legal values are 0 to 23.

  - ### MINUTE

    public static final DateFormat.Field MINUTE
    Constant identifying the minute field.

  - ### SECOND

    public static final DateFormat.Field SECOND
    Constant identifying the second field.

  - ### MILLISECOND

    public static final DateFormat.Field MILLISECOND
    Constant identifying the millisecond field.

  - ### DAY_OF_WEEK

    public static final DateFormat.Field DAY_OF_WEEK
    Constant identifying the day of week field.

  - ### DAY_OF_YEAR

    public static final DateFormat.Field DAY_OF_YEAR
    Constant identifying the day of year field.

  - ### DAY_OF_WEEK_IN_MONTH

    public static final DateFormat.Field DAY_OF_WEEK_IN_MONTH
    Constant identifying the day of week field.

  - ### WEEK_OF_YEAR

    public static final DateFormat.Field WEEK_OF_YEAR
    Constant identifying the week of year field.

  - ### WEEK_OF_MONTH

    public static final DateFormat.Field WEEK_OF_MONTH
    Constant identifying the week of month field.

  - ### AM_PM

    public static final DateFormat.Field AM_PM
    Constant identifying the time of day indicator (e.g. "a.m." or "p.m.") field.

  - ### HOUR1

    public static final DateFormat.Field HOUR1
    Constant identifying the hour field, where the legal values are 1 to 12.

  - ### HOUR0

    public static final DateFormat.Field HOUR0
    Constant identifying the hour field, where the legal values are 0 to 11.

  - ### TIME_ZONE

    public static final DateFormat.Field TIME_ZONE
    Constant identifying the time zone field.

- ## Constructor Details

  - ### Field

    protected Field(String name, int calendarField)
    Creates a `Field`.
    Parameters:
      `name` - the name of the `Field`
      `calendarField` - the `Calendar` constant this `Field` corresponds to; any value, even one outside the range of legal `Calendar` values may be used, but `-1` should be used for values that don't correspond to legal `Calendar` values

- ## Method Details

  - ### ofCalendarField

    public static DateFormat.Field ofCalendarField(int calendarField)
    Returns the `Field` constant that corresponds to the `Calendar` constant `calendarField`. If there is no direct mapping between the `Calendar` constant and a `Field`, null is returned.
    Parameters:
      `calendarField` - Calendar field constant
    Returns:
      Field instance representing calendarField.
    Throws:
      `IllegalArgumentException` - if `calendarField` is not the value of a `Calendar` field constant.
    See Also:
      - `Calendar`

  - ### getCalendarField

    public int getCalendarField()
    Returns the `Calendar` field associated with this attribute. For example, if this represents the hours field of a `Calendar`, this method would return `Calendar.HOUR`. The return value of `-1` guarantees that this field does not represent any corresponding constant in `Calendar`.
    Implementation Requirements:
      The default implementation always returns `-1` if it does not represent any corresponding constant in `Calendar`.
    Returns:
      Calendar constant for this field
    See Also:
      - `Calendar`

  - ### readResolve

    protected Object readResolve() throws InvalidObjectException
    Resolves instances being deserialized to the predefined constants.
    Overrides:
      `readResolve` in class `AttributedCharacterIterator.Attribute`
    Returns:
      resolved DateFormat.Field constant
    Throws:
      `InvalidObjectException` - if the constant could not be resolved.