C
- the type of the implementing subclasspublic abstract class Chrono<C extends Chrono<C>> extends java.lang.Object implements java.lang.Comparable<Chrono<?>>
The main date and time API is built on the ISO calendar system. This class operates behind the scenes to represent the general concept of a calendar system. For example, the Japanese, Minguo, Thai Buddhist and others.
Most other calendar systems also operate on the shared concepts of year, month and day,
linked to the cycles of the Earth around the Sun, and the Moon around the Earth.
These shared concepts are defined by ChronoField
and are availalbe
for use by any Chrono
implementation:
LocalDate isoDate = ... ChronoLocalDate<ThaiBuddhistChrono> thaiDate = ... int isoYear = isoDate.get(ChronoField.YEAR); int thaiYear = thaiDate.get(ChronoField.YEAR);As shown, although the date objects are in different calendar systems, represented by different
Chrono
instances, both can be queried using the same constant on ChronoField
.
For a full discussion of the implications of this, see ChronoLocalDate
.
In general, the advice is to use the known ISO-based LocalDate
, rather than
ChronoLocalDate
.
While a Chrono
object typically uses ChronoField
and is based on
an era, year-of-era, month-of-year, day-of-month model of a date, this is not required.
A Chrono
instance may represent a totally different kind of calendar system,
such as the Mayan.
In practical terms, the Chrono
instance also acts as a factory.
The of(String)
method allows an instance to be looked up by identifier,
while the ofLocale(Locale)
method allows lookup by locale.
The Chrono
instance provides a set of methods to create ChronoLocalDate
instances.
The date classes are used to manipulate specific dates.
dateNow()
dateNow(clock)
dateNow(zone)
date(yearProleptic, month, day)
date(era, yearOfEra, month, day)
dateYearDay(yearProleptic, dayOfYear)
dateYearDay(era, yearOfEra, dayOfYear)
date(TemporalAccessor)
Adding New Calendars
The set of available chronologies can be extended by applications. Adding a new calendar system requires the writing of an implementation ofChrono
, ChronoLocalDate
and Era
.
The majority of the logic specific to the calendar system will be in
ChronoLocalDate
. The Chrono
subclass acts as a factory.
To permit the discovery of additional chronologies, the ServiceLoader
is used. A file must be added to the META-INF/services
directory with the
name 'java.time.temporal.Chrono' listing the implementation classes.
See the ServiceLoader for more details on service loading.
For lookup by id or calendarType, the system provided calendars are found
first followed by application provided calendars.
Each chronology must define a chronology ID that is unique within the system. If the chronology represents a calendar system defined by the Unicode Locale Data Markup Language (LDML) specification then that calendar type should also be specified.
Modifier | Constructor and Description |
---|---|
protected |
Chrono()
Creates an instance.
|
Modifier and Type | Method and Description |
---|---|
int |
compareTo(Chrono<?> other)
Compares this chronology to another chronology.
|
ChronoLocalDate<C> |
date(Era<C> era,
int yearOfEra,
int month,
int dayOfMonth)
Obtains a local date in this chronology from the era, year-of-era,
month-of-year and day-of-month fields.
|
abstract ChronoLocalDate<C> |
date(int prolepticYear,
int month,
int dayOfMonth)
Obtains a local date in this chronology from the proleptic-year,
month-of-year and day-of-month fields.
|
abstract ChronoLocalDate<C> |
date(TemporalAccessor temporal)
Obtains a local date in this chronology from another temporal object.
|
ChronoLocalDate<C> |
dateNow()
Obtains the current local date in this chronology from the system clock in the default time-zone.
|
ChronoLocalDate<C> |
dateNow(Clock clock)
Obtains the current local date in this chronology from the specified clock.
|
ChronoLocalDate<C> |
dateNow(ZoneId zone)
Obtains the current local date in this chronology from the system clock in the specified time-zone.
|
static <R extends Chrono<R>> |
dateTime(ChronoLocalDate<R> date,
LocalTime time)
Obtains a local date-time from the a date and time.
|
ChronoLocalDate<C> |
dateYearDay(Era<C> era,
int yearOfEra,
int dayOfYear)
Obtains a local date in this chronology from the era, year-of-era and
day-of-year fields.
|
abstract ChronoLocalDate<C> |
dateYearDay(int prolepticYear,
int dayOfYear)
Obtains a local date in this chronology from the proleptic-year and
day-of-year fields.
|
boolean |
equals(java.lang.Object obj)
Checks if this chronology is equal to another chronology.
|
abstract Era<C> |
eraOf(int eraValue)
Creates the chronology era object from the numeric value.
|
abstract java.util.List<Era<C>> |
eras()
Gets the list of eras for the chronology.
|
static Chrono<?> |
from(TemporalAccessor temporal)
Obtains an instance of
Chrono from a temporal object. |
static java.util.Set<Chrono<?>> |
getAvailableChronologies()
Returns the available chronologies.
|
abstract java.lang.String |
getCalendarType()
Gets the calendar type of the underlying calendar system.
|
abstract java.lang.String |
getId()
Gets the ID of the chronology.
|
java.lang.String |
getText(TextStyle style,
java.util.Locale locale)
Gets the textual representation of this chronology.
|
int |
hashCode()
A hash code for this chronology.
|
abstract boolean |
isLeapYear(long prolepticYear)
Checks if the specified year is a leap year.
|
ChronoLocalDateTime<C> |
localDateTime(TemporalAccessor temporal)
Obtains a local date-time in this chronology from another temporal object.
|
static Chrono<?> |
of(java.lang.String id)
Obtains an instance of
Chrono from a chronology ID or
calendar system type. |
static Chrono<?> |
ofLocale(java.util.Locale locale)
Obtains an instance of
Chrono from a locale. |
abstract int |
prolepticYear(Era<C> era,
int yearOfEra)
Calculates the proleptic-year given the era and year-of-era.
|
abstract ValueRange |
range(ChronoField field)
Gets the range of valid values for the specified field.
|
java.lang.String |
toString()
Outputs this chronology as a
String , using the ID. |
ChronoZonedDateTime<C> |
zonedDateTime(Instant instant,
ZoneId zone)
Obtains a zoned date-time in this chronology from an
Instant . |
ChronoZonedDateTime<C> |
zonedDateTime(TemporalAccessor temporal)
Obtains a zoned date-time in this chronology from another temporal object.
|
public static Chrono<?> from(TemporalAccessor temporal)
Chrono
from a temporal object.
A TemporalAccessor
represents some form of date and time information.
This factory converts the arbitrary temporal object to an instance of Chrono
.
If the specified temporal object does not have a chronology, ISOChrono
is returned.
The conversion will obtain the chronology using Queries.chrono()
.
This method matches the signature of the functional interface TemporalQuery
allowing it to be used in queries via method reference, Chrono::from
.
temporal
- the temporal to convert, not nullDateTimeException
- if unable to convert to an Chrono
public static Chrono<?> ofLocale(java.util.Locale locale)
Chrono
from a locale.
The locale can be used to identify a calendar.
This uses Locale.getUnicodeLocaleType(String)
to obtain the "ca" key
to identify the calendar system.
If the locale does not contain calendar system information, the standard ISO calendar system is used.
locale
- the locale to use to obtain the calendar system, not nullDateTimeException
- if the locale-specified calendar cannot be foundpublic static Chrono<?> of(java.lang.String id)
Chrono
from a chronology ID or
calendar system type.
This returns a chronology based on either the ID or the type.
The chronology ID
uniquely identifies the chronology.
The calendar system type
is defined by the LDML specification.
The chronology may be a system chronology or a chronology provided by the application via ServiceLoader configuration.
Since some calendars can be customized, the ID or type typically refers to the default customization. For example, the Gregorian calendar can have multiple cutover dates from the Julian, but the lookup only provides the default cutover date.
id
- the chronology ID or calendar system type, not nullDateTimeException
- if the chronology cannot be foundpublic static java.util.Set<Chrono<?>> getAvailableChronologies()
Each returned Chrono
is available for use in the system.
The set of chronologies includes the system chronologies and
any chronologies provided by the application via ServiceLoader
configuration.
public static <R extends Chrono<R>> ChronoLocalDateTime<R> dateTime(ChronoLocalDate<R> date, LocalTime time)
This combines a ChronoLocalDate
, which provides the Chrono
,
with a LocalTime
to produce a ChronoLocalDateTime
.
This method is intended for chronology implementations. It uses a standard implementation that is shared for all chronologies.
R
- the chronology of the datedate
- the date, not nulltime
- the time, not nullpublic abstract java.lang.String getId()
The ID uniquely identifies the Chrono
.
It can be used to lookup the Chrono
using of(String)
.
getCalendarType()
public abstract java.lang.String getCalendarType()
The calendar type is an identifier defined by the
Unicode Locale Data Markup Language (LDML) specification.
It can be used to lookup the Chrono
using of(String)
.
It can also be used as part of a locale, accessible via
Locale.getUnicodeLocaleType(String)
with the key 'ca'.
getId()
public ChronoLocalDate<C> date(Era<C> era, int yearOfEra, int month, int dayOfMonth)
era
- the era of the correct type for the chronology, not nullyearOfEra
- the chronology year-of-eramonth
- the chronology month-of-yeardayOfMonth
- the chronology day-of-monthDateTimeException
- if unable to create the datepublic abstract ChronoLocalDate<C> date(int prolepticYear, int month, int dayOfMonth)
prolepticYear
- the chronology proleptic-yearmonth
- the chronology month-of-yeardayOfMonth
- the chronology day-of-monthDateTimeException
- if unable to create the datepublic ChronoLocalDate<C> dateYearDay(Era<C> era, int yearOfEra, int dayOfYear)
era
- the era of the correct type for the chronology, not nullyearOfEra
- the chronology year-of-eradayOfYear
- the chronology day-of-yearDateTimeException
- if unable to create the datepublic abstract ChronoLocalDate<C> dateYearDay(int prolepticYear, int dayOfYear)
prolepticYear
- the chronology proleptic-yeardayOfYear
- the chronology day-of-yearDateTimeException
- if unable to create the datepublic abstract ChronoLocalDate<C> date(TemporalAccessor temporal)
This creates a date in this chronology based on the specified TemporalAccessor
.
The standard mechanism for conversion between date types is the
local epoch-day
field.
temporal
- the temporal object to convert, not nullDateTimeException
- if unable to create the datepublic ChronoLocalDate<C> dateNow()
This will query the system clock
in the default
time-zone to obtain the current date.
Using this method will prevent the ability to use an alternate clock for testing because the clock is hard-coded.
This implementation uses dateNow(Clock)
.
DateTimeException
- if unable to create the datepublic ChronoLocalDate<C> dateNow(ZoneId zone)
This will query the system clock
to obtain the current date.
Specifying the time-zone avoids dependence on the default time-zone.
Using this method will prevent the ability to use an alternate clock for testing because the clock is hard-coded.
zone
- the zone ID to use, not nullDateTimeException
- if unable to create the datepublic ChronoLocalDate<C> dateNow(Clock clock)
This will query the specified clock to obtain the current date - today.
Using this method allows the use of an alternate clock for testing.
The alternate clock may be introduced using dependency injection
.
clock
- the clock to use, not nullDateTimeException
- if unable to create the datepublic ChronoLocalDateTime<C> localDateTime(TemporalAccessor temporal)
This creates a date-time in this chronology based on the specified TemporalAccessor
.
The date of the date-time should be equivalent to that obtained by calling
date(TemporalAccessor)
.
The standard mechanism for conversion between time types is the
nano-of-day
field.
temporal
- the temporal object to convert, not nullDateTimeException
- if unable to create the date-timepublic ChronoZonedDateTime<C> zonedDateTime(TemporalAccessor temporal)
This creates a date-time in this chronology based on the specified TemporalAccessor
.
This should obtain a ZoneId
using ZoneId.from(TemporalAccessor)
.
The date-time should be obtained by obtaining an Instant
.
If that fails, the local date-time should be used.
temporal
- the temporal object to convert, not nullDateTimeException
- if unable to create the date-timepublic ChronoZonedDateTime<C> zonedDateTime(Instant instant, ZoneId zone)
Instant
.
This creates a zoned date-time with the same instant as that specified.
instant
- the instant to create the date-time from, not nullzone
- the time-zone, not nullDateTimeException
- if the result exceeds the supported rangepublic abstract boolean isLeapYear(long prolepticYear)
A leap-year is a year of a longer length than normal. The exact meaning is determined by the chronology according to the following constraints.
prolepticYear
- the proleptic-year to check, not validated for rangepublic abstract int prolepticYear(Era<C> era, int yearOfEra)
This combines the era and year-of-era into the single proleptic-year field.
era
- the era of the correct type for the chronology, not nullyearOfEra
- the chronology year-of-eraDateTimeException
- if unable to convertpublic abstract Era<C> eraOf(int eraValue)
The era is, conceptually, the largest division of the time-line. Most calendar systems have a single epoch dividing the time-line into two eras. However, some have multiple eras, such as one for the reign of each leader. The exact meaning is determined by the chronology according to the following constraints.
The era in use at 1970-01-01 must have the value 1. Later eras must have sequentially higher values. Earlier eras must have sequentially lower values. Each chronology must refer to an enum or similar singleton to provide the era values.
This method returns the singleton era of the correct type for the specified era value.
eraValue
- the era valueDateTimeException
- if unable to create the erapublic abstract java.util.List<Era<C>> eras()
Most calendar systems have an era, within which the year has meaning. If the calendar system does not support the concept of eras, an empty list must be returned.
public abstract ValueRange range(ChronoField field)
All fields can be expressed as a long
integer.
This method returns an object that describes the valid range for that value.
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.
This method will return a result whether or not the chronology supports the field.
field
- the field to get the range for, not nullDateTimeException
- if the range for the field cannot be obtainedpublic java.lang.String getText(TextStyle style, java.util.Locale locale)
This returns the textual name used to identify the chronology. The parameters control the style of the returned text and the locale.
style
- the style of the text required, not nulllocale
- the locale to use, not nullpublic int compareTo(Chrono<?> other)
The comparison order first by the chronology ID string, then by any
additional information specific to the subclass.
It is "consistent with equals", as defined by Comparable
.
The default implementation compares the chronology ID. Subclasses must compare any additional state that they store.
compareTo
in interface java.lang.Comparable<Chrono<?>>
other
- the other chronology to compare to, not nullpublic boolean equals(java.lang.Object obj)
The comparison is based on the entire state of the object.
The default implementation checks the type and calls compareTo(Chrono)
.
equals
in class java.lang.Object
obj
- the object to check, null returns falseObject.hashCode()
,
HashMap
public int hashCode()
The default implementation is based on the ID and class. Subclasses should add any additional state that they store.
hashCode
in class java.lang.Object
Object.equals(java.lang.Object)
,
System.identityHashCode(java.lang.Object)
public java.lang.String toString()
String
, using the ID.toString
in class java.lang.Object