Home Reference Source
public class | source

ChronoField

Extends:

TemporalField → ChronoField

A standard set of fields.

This set of fields provide field-based access to manipulate a date, time or date-time. The standard set of fields can be extended by implementing TemporalField.

These fields are intended to be applicable in multiple calendar systems. For example, most non-ISO calendar systems define dates as a year, month and day, just with slightly different rules. The documentation of each field explains how it operates.

Static properties:

  • ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH: This represents concept of the count of days within the period of a week where the weeks are aligned to the start of the month. This field is typically used with ALIGNED_WEEK_OF_MONTH.

  • ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR: This represents concept of the count of days within the period of a week where the weeks are aligned to the start of the year. This field is typically used with ALIGNED_WEEK_OF_YEAR.

  • ChronoField.ALIGNED_WEEK_OF_MONTH: This represents concept of the count of weeks within the period of a month where the weeks are aligned to the start of the month. This field is typically used with ALIGNED_DAY_OF_WEEK_IN_MONTH.

  • ChronoField.ALIGNED_WEEK_OF_YEAR: This represents concept of the count of weeks within the period of a year where the weeks are aligned to the start of the year. This field is typically used with ALIGNED_DAY_OF_WEEK_IN_YEAR.

  • ChronoField.AMPM_OF_DAY: This counts the AM/PM within the day, from 0 (AM) to 1 (PM).

  • ChronoField.CLOCK_HOUR_OF_AMPM: This counts the hour within the AM/PM, from 1 to 12. This is the hour that would be observed on a standard 12-hour analog wall clock.

  • ChronoField.CLOCK_HOUR_OF_DAY: This counts the hour within the AM/PM, from 1 to 24. This is the hour that would be observed on a 24-hour analog wall clock.

  • ChronoField.DAY_OF_MONTH: This represents the concept of the day within the month. In the default ISO calendar system, this has values from 1 to 31 in most months. April, June, September, November have days from 1 to 30, while February has days from 1 to 28, or 29 in a leap year.

  • ChronoField.DAY_OF_WEEK: This represents the standard concept of the day of the week. In the default ISO calendar system, this has values from Monday (1) to Sunday (7). The DayOfWeek class can be used to interpret the result.

  • ChronoField.DAY_OF_YEAR: This represents the concept of the day within the year. In the default ISO calendar system, this has values from 1 to 365 in standard years and 1 to 366 in leap years.

  • ChronoField.EPOCH_DAY: This field is the sequential count of days where 1970-01-01 (ISO) is zero. Note that this uses the local time-line, ignoring offset and time-zone.

  • ChronoField.ERA: This represents the concept of the era, which is the largest division of the time-line. This field is typically used with YEAR_OF_ERA.

    In the default ISO calendar system, there are two eras defined, 'BCE' and 'CE'. The era 'CE' is the one currently in use and year-of-era runs from 1 to the maximum value. The era 'BCE' is the previous era, and the year-of-era runs backwards.

  • ChronoField.HOUR_OF_AMPM: This counts the hour within the AM/PM, from 0 to 11. This is the hour that would be observed on a standard 12-hour digital clock.

  • ChronoField.HOUR_OF_DAY: This counts the hour within the day, from 0 to 23. This is the hour that would be observed on a standard 24-hour digital clock.

  • ChronoField.INSTANT_SECONDS: This represents the concept of the sequential count of seconds where 1970-01-01T00:00Z (ISO) is zero. This field may be used with NANO_OF_DAY to represent the fraction of the day.

    An Instant represents an instantaneous point on the time-line. On their own they have no elements which allow a local date-time to be obtained. Only when paired with an offset or time-zone can the local date or time be found. This field allows the seconds part of the instant to be queried.

  • ChronoField.MICRO_OF_DAY: This counts the microsecond within the day, from 0 to (24 60 60 * 1,000,000) - 1.

    This field is used to represent the micro-of-day handling any fraction of the second. Implementations of TemporalAccessor should provide a value for this field if they can return a value for SECOND_OF_DAY filling unknown precision with zero.

    When this field is used for setting a value, it should behave in the same way as setting NANO_OF_DAY with the value multiplied by 1,000.

  • ChronoField.MICRO_OF_SECOND: This counts the microsecond within the second, from 0 to 999,999.

    This field is used to represent the micro-of-second handling any fraction of the second. Implementations of TemporalAccessor should provide a value for this field if they can return a value for SECOND_OF_MINUTE, SECOND_OF_DAY or INSTANT_SECONDS filling unknown precision with zero.

  • ChronoField.MILLI_OF_DAY: This counts the millisecond within the day, from 0 to (24 60 60 * 1,000) - 1.

    This field is used to represent the milli-of-day handling any fraction of the second. Implementations of TemporalAccessor should provide a value for this field if they can return a value for SECOND_OF_DAY filling unknown precision with zero.

    When this field is used for setting a value, it should behave in the same way as setting NANO_OF_DAY with the value multiplied by 1,000,000.

  • ChronoField.MILLI_OF_SECOND: This counts the millisecond within the second, from 0 to 999.

    This field is used to represent the milli-of-second handling any fraction of the second. Implementations of TemporalAccessor should provide a value for this field if they can return a value for SECOND_OF_MINUTE, SECOND_OF_DAY or INSTANT_SECONDS filling unknown precision with zero.

    When this field is used for setting a value, it should behave in the same way as setting NANO_OF_SECOND with the value multiplied by 1,000,000.

  • ChronoField.MINUTE_OF_DAY: This counts the minute within the day, from 0 to (24 * 60) - 1.

  • ChronoField.MINUTE_OF_HOUR: This counts the minute within the hour, from 0 to 59.

  • ChronoField.MONTH_OF_YEAR: The month-of-year, such as March. This represents the concept of the month within the year. In the default ISO calendar system, this has values from January (1) to December (12).

  • ChronoField.NANO_OF_DAY: This counts the nanosecond within the day, from 0 to (24 60 60 * 1,000,000,000) - 1.

    This field is used to represent the nano-of-day handling any fraction of the second. Implementations of TemporalAccessor should provide a value for this field if they can return a value for SECOND_OF_DAY filling unknown precision with zero.

  • ChronoField.NANO_OF_SECOND: This counts the nanosecond within the second, from 0 to 999,999,999.

    This field is used to represent the nano-of-second handling any fraction of the second. Implementations of TemporalAccessor should provide a value for this field if they can return a value for SECOND_OF_MINUTE, SECOND_OF_DAY or INSTANT_SECONDS filling unknown precision with zero.

    When this field is used for setting a value, it should set as much precision as the object stores, using integer division to remove excess precision. For example, if the TemporalAccessor stores time to millisecond precision, then the nano-of-second must be divided by 1,000,000 before replacing the milli-of-second.

  • ChronoField.OFFSET_SECONDS: This represents the concept of the offset in seconds of local time from UTC/Greenwich.

    A ZoneOffset represents the period of time that local time differs from UTC/Greenwich. This is usually a fixed number of hours and minutes. It is equivalent to the total amount of the offset in seconds. For example, during the winter Paris has an offset of +01:00, which is 3600 seconds.

  • ChronoField.PROLEPTIC_MONTH: The proleptic-month, which counts months sequentially from year 0.

    The first month in year zero has the value zero. The value increase for later months and decrease for earlier ones. Note that this uses the local time-line, ignoring offset and time-zone.

  • ChronoField.SECOND_OF_DAY: This counts the second within the day, from 0 to (24 60 60) - 1.

  • ChronoField.SECOND_OF_MINUTE: This counts the second within the minute, from 0 to 59.

  • ChronoField.YEAR: The proleptic year, such as 2012. This represents the concept of the year, counting sequentially and using negative numbers. The proleptic year is not interpreted in terms of the era.

    The standard mental model for a date is based on three concepts - year, month and day. These map onto the YEAR, MONTH_OF_YEAR and DAY_OF_MONTH fields. Note that there is no reference to eras. The full model for a date requires four concepts - era, year, month and day. These map onto the ERA, YEAR_OF_ERA, MONTH_OF_YEAR and DAY_OF_MONTH fields. Whether this field or YEAR_OF_ERA is used depends on which mental model is being used.

  • ChronoField.YEAR_OF_ERA: This represents the concept of the year within the era. This field is typically used with ERA. The standard mental model for a date is based on three concepts - year, month and day. These map onto the YEAR, MONTH_OF_YEAR and DAY_OF_MONTH fields. Note that there is no reference to eras. The full model for a date requires four concepts - era, year, month and day. These map onto the ERA, YEAR_OF_ERA, MONTH_OF_YEAR and DAY_OF_MONTH fields. Whether this field or YEAR is used depends on which mental model is being used.

    In the default ISO calendar system, there are two eras defined, 'BCE' and 'CE'. The era 'CE' is the one currently in use and year-of-era runs from 1 to the maximum value. The era 'BCE' is the previous era, and the year-of-era runs backwards.

    For example, subtracting a year each time yield the following:

    • year-proleptic 2 = 'CE' year-of-era 2
    • year-proleptic 1 = 'CE' year-of-era 1
    • year-proleptic 0 = 'BCE' year-of-era 1
    • year-proleptic -1 = 'BCE' year-of-era 2

      Note that the ISO-8601 standard does not actually define eras. Note also that the ISO eras do not align with the well-known AD/BC eras due to the change between the Julian and Gregorian calendar systems.

Method Summary

Public Methods
public

adjustInto(temporal: Temporal, newValue: number): Temporal

public
public

checkValidIntValue(value: number): number

Checks that the specified value is valid and fits in an int.

public

checkValidValue(value: number): number

Checks that the specified value is valid for this field.

public

displayName(): string

public

equals(other: *): boolean

public

getFrom(temporal: TemporalAccesor): number

public

isDateBased(): boolean

public

isSupportedBy(temporal: TemporalAccesor): boolean

public

isTimeBased(): boolean

public

name(): string

public
public
public
public

toString(): string

Inherited Summary

From class TemporalField
public

adjustInto(temporal: Temporal, newValue: number): Temporal

Returns a copy of the specified temporal object with the value of this field set.

public

Gets the unit that the field is measured in.

public

displayName(): string

public

equals(other: *): boolean

public

getFrom(temporal: TemporalAccesor): number

Gets the value of this field from the specified temporal object.

public

isDateBased(): boolean

Checks if this field represents a component of a date.

public

isSupportedBy(temporal: TemporalAccesor): boolean

Checks if this field is supported by the temporal object.

public

isTimeBased(): boolean

Checks if this field represents a component of a time.

public

name(): string

public

Gets the range of valid values for the field.

public

Get the range of valid values for this field using the temporal object to refine the result.

public

Gets the range that the field is bound by.

Public Methods

public adjustInto(temporal: Temporal, newValue: number): Temporal source

Returns a copy of the specified temporal object with the value of this field set.

This returns a new temporal object based on the specified one with the value for this field changed. For example, on a LocalDate, this could be used to set the year, month or day-of-month. The returned object has the same observable type as the specified object.

In some cases, changing a field is not fully defined. For example, if the target object is a date representing the 31st January, then changing the month to February would be unclear. In cases like this, the implementation is responsible for resolving the result. Typically it will choose the previous valid date, which would be the last valid day of February in this example.

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

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

Implementations should perform any queries or calculations using the fields available in ChronoField. If the field is not supported a DateTimeException must be thrown.

Implementations must not alter the specified temporal object. Instead, an adjusted copy of the original must be returned. This provides equivalent, safe behavior for immutable and mutable implementations.

Override:

TemporalField#adjustInto

Params:

NameTypeAttributeDescription
temporal Temporal
  • nullable: false

the temporal object to adjust.

newValue number
  • nullable: false

the new value of the field.

Return:

Temporal

the adjusted temporal object.

Throw:

DateTimeException

if the field cannot be set.

public baseUnit(): TemporalUnit source

Gets the unit that the field is measured in.

The unit of the field is the period that varies within the range. For example, in the field 'MonthOfYear', the unit is 'Months'. See also rangeUnit.

Override:

TemporalField#baseUnit

Return:

TemporalUnit

the period unit defining the base unit of the field.

public checkValidIntValue(value: number): number source

Checks that the specified value is valid and fits in an int.

This validates that the value is within the outer range of valid values returned by range. It also checks that all valid values are within the bounds of an int.

This method checks against the range of the field in the ISO-8601 calendar system.

Params:

NameTypeAttributeDescription
value number

the value to check.

Return:

number

the value that was passed in.

public checkValidValue(value: number): number source

Checks that the specified value is valid for this field.

This validates that the value is within the outer range of valid values returned by range.

This method checks against the range of the field in the ISO-8601 calendar system.

Params:

NameTypeAttributeDescription
value number
  • nullable: false

the value to check.

Return:

number

the value that was passed in.

public displayName(): string source

Override:

TemporalField#displayName

Return:

string

public equals(other: *): boolean source

Override:

TemporalField#equals

Params:

NameTypeAttributeDescription
other *

Return:

boolean

public getFrom(temporal: TemporalAccesor): number source

Gets the value of this field from the specified temporal object.

This queries the temporal object for the value of this field.

There are two equivalent ways of using this method. The first is to invoke this method directly. The second is to use TemporalAccessor#get:

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

Implementations should perform any queries or calculations using the fields available in ChronoField. If the field is not supported a DateTimeException must be thrown.

Override:

TemporalField#getFrom

Params:

NameTypeAttributeDescription
temporal TemporalAccesor
  • nullable: false

the temporal object to query.

Return:

number

the value of this field.

Throw:

DateTimeException

if a value for the field cannot be obtained.

public isDateBased(): boolean source

Checks if this field represents a component of a date.

Override:

TemporalField#isDateBased

Return:

boolean

true if it is a component of a date, false otherwise.

public isSupportedBy(temporal: TemporalAccesor): boolean source

Checks if this field is supported by the temporal object.

This determines whether the temporal accessor supports this field. If this returns false, the the temporal cannot be queried for this field.

There are two equivalent ways of using this method. The first is to invoke this method directly. The second is to use TemporalAccessor#isSupported:

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

Implementations should determine whether they are supported using the fields available in ChronoField.

Override:

TemporalField#isSupportedBy

Params:

NameTypeAttributeDescription
temporal TemporalAccesor
  • nullable: false

the temporal object to query.

Return:

boolean

true if the date-time can be queried for this field, false if not.

public isTimeBased(): boolean source

Checks if this field represents a component of a time.

Override:

TemporalField#isTimeBased

Return:

boolean

true if it is a component of a time, false otherwise.

public name(): string source

Override:

TemporalField#name

Return:

string

public range(): ValueRange source

Gets the range of valid values for the field.

All fields can be expressed as an integer. This method returns an object that describes the valid range for that value. This method is generally only applicable to the ISO-8601 calendar system.

Note that the result only describes the minimum and maximum valid values and it is important not to read too much into them. For example, there could be values within the range that are invalid for the field.

Override:

TemporalField#range

Return:

ValueRange

the range of valid values for the field.

public rangeRefinedBy(temporal: TemporalAccessor): ValueRange source

Get the range of valid values for this field using the temporal object to refine the result.

This uses the temporal object to find the range of valid values for the field. This is similar to range, however this method refines the result using the temporal. For example, if the field is DAY_OF_MONTH the range method is not accurate as there are four possible month lengths, 28, 29, 30 and 31 days. Using this method with a date allows the range to be accurate, returning just one of those four options.

There are two equivalent ways of using this method. The first is to invoke this method directly. The second is to use TemporalAccessor#range:

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

Implementations should perform any queries or calculations using the fields available in ChronoField. If the field is not supported a DateTimeException must be thrown.

Override:

TemporalField#rangeRefinedBy

Params:

NameTypeAttributeDescription
temporal TemporalAccessor
  • nullable: false

the temporal object used to refine the result.

Return:

ValueRange

the range of valid values for this field.

Throw:

DateTimeException

if the range for the field cannot be obtained.

public rangeUnit(): TemporalUnit source

Gets the range that the field is bound by.

The range of the field is the period that the field varies within. For example, in the field 'MonthOfYear', the range is 'Years'. See also baseUnit.

The range is never null. For example, the 'Year' field is shorthand for 'YearOfForever'. It therefore has a unit of 'Years' and a range of 'Forever'.

Override:

TemporalField#rangeUnit

Return:

TemporalUnit

the period unit defining the range of the field.

public toString(): string source

Return:

string