Interface Chronology
-
- All Superinterfaces:
java.lang.Comparable<Chronology>
- All Known Implementing Classes:
AbstractChronology,IsoChronology
public interface Chronology extends java.lang.Comparable<Chronology>
A calendar system, used to organize and identify dates.The main date and time API is built on the ISO calendar system. The chronology 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
ChronoFieldand are available for use by anyChronologyimplementation:LocalDate isoDate = ... ThaiBuddhistDate 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 differentChronologyinstances, both can be queried using the same constant onChronoField. For a full discussion of the implications of this, seeChronoLocalDate. In general, the advice is to use the known ISO-basedLocalDate, rather thanChronoLocalDate.While a
Chronologyobject typically usesChronoFieldand is based on an era, year-of-era, month-of-year, day-of-month model of a date, this is not required. AChronologyinstance may represent a totally different kind of calendar system, such as the Mayan.In practical terms, the
Chronologyinstance also acts as a factory. TheAbstractChronology.of(String)method allows an instance to be looked up by identifier.The
Chronologyinstance provides a set of methods to createChronoLocalDateinstances. 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 ofChronology,ChronoLocalDateandEra. The majority of the logic specific to the calendar system will be in theChronoLocalDateimplementation. TheChronologyimplementation acts as a factory.Each chronology must define a chronology ID that is unique within the system. If the chronology represents a calendar system defined by the CLDR specification then the calendar type is the concatenation of the CLDR type and, if applicable, the CLDR variant,
This interface must be implemented with care to ensure other classes operate correctly. All implementations that can be instantiated must be final, immutable and thread-safe. Subclasses should be Serializable wherever possible.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description intcompareTo(Chronology other)Compares this chronology to another chronology.ChronoLocalDatedate(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.ChronoLocalDatedate(Era 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.ChronoLocalDatedate(TemporalAccessor temporal)Obtains a local date in this chronology from another temporal object.ChronoLocalDatedateEpochDay(long epochDay)Obtains a local date in this chronology from the epoch-day.ChronoLocalDatedateNow()Obtains the current local date in this chronology from the system clock in the default time-zone.ChronoLocalDatedateNow(Clock clock)Obtains the current local date in this chronology from the specified clock.ChronoLocalDatedateNow(ZoneId zone)Obtains the current local date in this chronology from the system clock in the specified time-zone.ChronoLocalDatedateYearDay(int prolepticYear, int dayOfYear)Obtains a local date in this chronology from the proleptic-year and day-of-year fields.ChronoLocalDatedateYearDay(Era era, int yearOfEra, int dayOfYear)Obtains a local date in this chronology from the era, year-of-era and day-of-year fields.booleanequals(java.lang.Object obj)Checks if this chronology is equal to another chronology.EraeraOf(int eraValue)Creates the chronology era object from the numeric value.java.util.List<Era>eras()Gets the list of eras for the chronology.java.lang.StringgetCalendarType()Gets the calendar type of the calendar system.java.lang.StringgetId()Gets the ID of the chronology.inthashCode()A hash code for this chronology.booleanisLeapYear(long prolepticYear)Checks if the specified year is a leap year.ChronoLocalDateTime<? extends ChronoLocalDate>localDateTime(TemporalAccessor temporal)Obtains a local date-time in this chronology from another temporal object.ChronoPeriodperiod(int years, int months, int days)Obtains a period for this chronology based on years, months and days.intprolepticYear(Era era, int yearOfEra)Calculates the proleptic-year given the era and year-of-era.ValueRangerange(ChronoField field)Gets the range of valid values for the specified field.ChronoLocalDateresolveDate(java.util.Map<TemporalField,java.lang.Long> fieldValues, ResolverStyle resolverStyle)Resolves parsedChronoFieldvalues into a date during parsing.java.lang.StringtoString()Outputs this chronology as aString.ChronoZonedDateTime<? extends ChronoLocalDate>zonedDateTime(Instant instant, ZoneId zone)Obtains aChronoZonedDateTimein this chronology from anInstant.ChronoZonedDateTime<? extends ChronoLocalDate>zonedDateTime(TemporalAccessor temporal)Obtains aChronoZonedDateTimein this chronology from another temporal object.
-
-
-
Method Detail
-
getId
java.lang.String getId()
Gets the ID of the chronology.The ID uniquely identifies the
Chronology. It can be used to lookup theChronologyusingAbstractChronology.of(String).- Returns:
- the chronology ID, not null
- See Also:
getCalendarType()
-
getCalendarType
@Nullable java.lang.String getCalendarType()
Gets the calendar type of the calendar system.The calendar type is an identifier defined by the CLDR and Unicode Locale Data Markup Language (LDML) specifications to uniquely identification a calendar. The
getCalendarTypeis the concatenation of the CLDR calendar type and the variant, if applicable, is appended separated by "-". The calendar type is used to lookup theChronologyusingAbstractChronology.of(String).- Returns:
- the calendar system type, null if the calendar is not defined by CLDR/LDML
- See Also:
getId()
-
date
ChronoLocalDate date(Era 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.The default implementation combines the era and year-of-era into a proleptic year before calling
date(int, int, int).- Parameters:
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-month- Returns:
- the local date in this chronology, not null
- Throws:
DateTimeException- if unable to create the datejava.lang.ClassCastException- if theerais not of the correct type for the chronology
-
date
ChronoLocalDate 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.- Parameters:
prolepticYear- the chronology proleptic-yearmonth- the chronology month-of-yeardayOfMonth- the chronology day-of-month- Returns:
- the local date in this chronology, not null
- Throws:
DateTimeException- if unable to create the date
-
dateYearDay
ChronoLocalDate dateYearDay(Era era, int yearOfEra, int dayOfYear)
Obtains a local date in this chronology from the era, year-of-era and day-of-year fields.The default implementation combines the era and year-of-era into a proleptic year before calling
dateYearDay(int, int).- Parameters:
era- the era of the correct type for the chronology, not nullyearOfEra- the chronology year-of-eradayOfYear- the chronology day-of-year- Returns:
- the local date in this chronology, not null
- Throws:
DateTimeException- if unable to create the datejava.lang.ClassCastException- if theerais not of the correct type for the chronology
-
dateYearDay
ChronoLocalDate dateYearDay(int prolepticYear, int dayOfYear)
Obtains a local date in this chronology from the proleptic-year and day-of-year fields.- Parameters:
prolepticYear- the chronology proleptic-yeardayOfYear- the chronology day-of-year- Returns:
- the local date in this chronology, not null
- Throws:
DateTimeException- if unable to create the date
-
dateEpochDay
ChronoLocalDate dateEpochDay(long epochDay)
Obtains a local date in this chronology from the epoch-day.The definition of
EPOCH_DAYis the same for all calendar systems, thus it can be used for conversion.- Parameters:
epochDay- the epoch day- Returns:
- the local date in this chronology, not null
- Throws:
DateTimeException- if unable to create the date
-
dateNow
ChronoLocalDate dateNow()
Obtains the current local date in this chronology from the system clock in the default time-zone.This will query the
system clockin 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.
The default implementation invokes
dateNow(Clock).- Returns:
- the current local date using the system clock and default time-zone, not null
- Throws:
DateTimeException- if unable to create the date
-
dateNow
ChronoLocalDate dateNow(ZoneId zone)
Obtains the current local date in this chronology from the system clock in the specified time-zone.This will query the
system clockto 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.
The default implementation invokes
dateNow(Clock).- Parameters:
zone- the zone ID to use, not null- Returns:
- the current local date using the system clock, not null
- Throws:
DateTimeException- if unable to create the date
-
dateNow
ChronoLocalDate dateNow(Clock clock)
Obtains the current local date in this chronology from the specified 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.The default implementation invokes
date(TemporalAccessor).- Parameters:
clock- the clock to use, not null- Returns:
- the current local date, not null
- Throws:
DateTimeException- if unable to create the date
-
date
ChronoLocalDate date(TemporalAccessor temporal)
Obtains a local date in this chronology from another temporal object.This obtains a date in this chronology based on the specified temporal. A
TemporalAccessorrepresents an arbitrary set of date and time information, which this factory converts to an instance ofChronoLocalDate.The conversion typically uses the
EPOCH_DAYfield, which is standardized across calendar systems.This method matches the signature of the functional interface
TemporalQueryallowing it to be used as a query via method reference,aChronology::date.- Parameters:
temporal- the temporal object to convert, not null- Returns:
- the local date in this chronology, not null
- Throws:
DateTimeException- if unable to create the date
-
localDateTime
ChronoLocalDateTime<? extends ChronoLocalDate> localDateTime(TemporalAccessor temporal)
Obtains a local date-time in this chronology from another temporal object.This obtains a date-time in this chronology based on the specified temporal. A
TemporalAccessorrepresents an arbitrary set of date and time information, which this factory converts to an instance ofChronoLocalDateTime.The conversion extracts and combines the
ChronoLocalDateand theLocalTimefrom the temporal object. Implementations are permitted to perform optimizations such as accessing those fields that are equivalent to the relevant objects. The result uses this chronology.This method matches the signature of the functional interface
TemporalQueryallowing it to be used as a query via method reference,aChronology::localDateTime.- Parameters:
temporal- the temporal object to convert, not null- Returns:
- the local date-time in this chronology, not null
- Throws:
DateTimeException- if unable to create the date-time
-
zonedDateTime
ChronoZonedDateTime<? extends ChronoLocalDate> zonedDateTime(TemporalAccessor temporal)
Obtains aChronoZonedDateTimein this chronology from another temporal object.This obtains a zoned date-time in this chronology based on the specified temporal. A
TemporalAccessorrepresents an arbitrary set of date and time information, which this factory converts to an instance ofChronoZonedDateTime.The conversion will first obtain a
ZoneIdfrom the temporal object, falling back to aZoneOffsetif necessary. It will then try to obtain anInstant, falling back to aChronoLocalDateTimeif necessary. The result will be either the combination ofZoneIdorZoneOffsetwithInstantorChronoLocalDateTime. Implementations are permitted to perform optimizations such as accessing those fields that are equivalent to the relevant objects. The result uses this chronology.This method matches the signature of the functional interface
TemporalQueryallowing it to be used as a query via method reference,aChronology::zonedDateTime.- Parameters:
temporal- the temporal object to convert, not null- Returns:
- the zoned date-time in this chronology, not null
- Throws:
DateTimeException- if unable to create the date-time
-
zonedDateTime
ChronoZonedDateTime<? extends ChronoLocalDate> zonedDateTime(Instant instant, ZoneId zone)
Obtains aChronoZonedDateTimein this chronology from anInstant.This obtains a zoned date-time with the same instant as that specified.
- Parameters:
instant- the instant to create the date-time from, not nullzone- the time-zone, not null- Returns:
- the zoned date-time, not null
- Throws:
DateTimeException- if the result exceeds the supported range
-
isLeapYear
boolean isLeapYear(long prolepticYear)
Checks if the specified year is a leap year.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.
- a leap-year must imply a year-length longer than a non leap-year.
- a chronology that does not support the concept of a year must return false.
- Parameters:
prolepticYear- the proleptic-year to check, not validated for range- Returns:
- true if the year is a leap year
-
prolepticYear
int prolepticYear(Era era, int yearOfEra)
Calculates the proleptic-year given the era and year-of-era.This combines the era and year-of-era into the single proleptic-year field.
If the chronology makes active use of eras, such as
JapaneseChronologythen the year-of-era will be validated against the era. For other chronologies, validation is optional.- Parameters:
era- the era of the correct type for the chronology, not nullyearOfEra- the chronology year-of-era- Returns:
- the proleptic-year
- Throws:
DateTimeException- if unable to convert to a proleptic-year, such as if the year is invalid for the erajava.lang.ClassCastException- if theerais not of the correct type for the chronology
-
eraOf
Era eraOf(int eraValue)
Creates the chronology era object from the numeric value.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.
- Parameters:
eraValue- the era value- Returns:
- the calendar system era, not null
- Throws:
DateTimeException- if unable to create the era
-
eras
java.util.List<Era> eras()
Gets the list of eras for the chronology.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.
- Returns:
- the list of eras for the chronology, may be immutable, not null
-
range
ValueRange range(ChronoField field)
Gets the range of valid values for the specified field.All fields can be expressed as a
longinteger. 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.
- Parameters:
field- the field to get the range for, not null- Returns:
- the range of valid values for the field, not null
- Throws:
DateTimeException- if the range for the field cannot be obtained
-
resolveDate
@Nullable ChronoLocalDate resolveDate(java.util.Map<TemporalField,java.lang.Long> fieldValues, ResolverStyle resolverStyle)
Resolves parsedChronoFieldvalues into a date during parsing.Most
TemporalFieldimplementations are resolved using the resolve method on the field. By contrast, theChronoFieldclass defines fields that only have meaning relative to the chronology. As such,ChronoFielddate fields are resolved here in the context of a specific chronology.The default implementation, which explains typical resolve behaviour, is provided in
AbstractChronology.- Parameters:
fieldValues- the map of fields to values, which can be updated, not nullresolverStyle- the requested type of resolve, not null- Returns:
- the resolved date, null if insufficient information to create a date
- Throws:
DateTimeException- if the date cannot be resolved, typically because of a conflict in the input data
-
period
ChronoPeriod period(int years, int months, int days)
Obtains a period for this chronology based on years, months and days.This returns a period tied to this chronology using the specified years, months and days. All supplied chronologies use periods based on years, months and days, however the
ChronoPeriodAPI allows the period to be represented using other units.The default implementation returns an implementation class suitable for most calendar systems. It is based solely on the three units. Normalization, addition and subtraction derive the number of months in a year from the
range(ChronoField). If the number of months within a year is fixed, then the calculation approach for addition, subtraction and normalization is slightly different.If implementing an unusual calendar system that is not based on years, months and days, or where you want direct control, then the
ChronoPeriodinterface must be directly implemented.The returned period is immutable and thread-safe.
- Parameters:
years- the number of years, may be negativemonths- the number of years, may be negativedays- the number of years, may be negative- Returns:
- the period in terms of this chronology, not null
-
compareTo
int compareTo(Chronology other)
Compares this chronology to another chronology.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.- Specified by:
compareToin interfacejava.lang.Comparable<Chronology>- Parameters:
other- the other chronology to compare to, not null- Returns:
- the comparator value, negative if less, positive if greater
-
equals
boolean equals(@Nullable java.lang.Object obj)Checks if this chronology is equal to another chronology.The comparison is based on the entire state of the object.
- Overrides:
equalsin classjava.lang.Object- Parameters:
obj- the object to check, null returns false- Returns:
- true if this is equal to the other chronology
-
hashCode
int hashCode()
A hash code for this chronology.The hash code should be based on the entire state of the object.
- Overrides:
hashCodein classjava.lang.Object- Returns:
- a suitable hash code
-
toString
java.lang.String toString()
Outputs this chronology as aString.The format should include the entire state of the object.
- Overrides:
toStringin classjava.lang.Object- Returns:
- a string representation of this chronology, not null
-
-