This class is used by applications seeking to handle dates in non-ISO calendar systems. For example, the Japanese, Minguo, Thai Buddhist and others.
ChronoLocalDate is built on the generic concepts of year, month and day.
The calendar system, represented by a java., expresses the relationship between
the fields and this class allows the resulting date to be manipulated.
Note that not all calendar systems are suitable for use with this class. For example, the Mayan calendar uses a system that bears no relation to years, months and days.
The API design encourages the use of LocalDate for the majority of the application.
This includes code to read and write from a persistent data store, such as a database,
and to send dates and times across a network. The ChronoLocalDate instance is then used
at the user interface level to deal with localized input/output.
Example:
System.out.printf("Example()%n");
// Enumerate the list of available calendars and print today for each
Set<Chronology> chronos = Chronology.getAvailableChronologies();
for (Chronology chrono : chronos) {
ChronoLocalDate date = chrono.dateNow();
System.out.printf(" %20s: %s%n", chrono.getID(), date.toString());
}
// Print the Hijrah date and calendar
ChronoLocalDate date = Chronology.of("Hijrah").dateNow();
int day = date.get(ChronoField.DAY_OF_MONTH);
int dow = date.get(ChronoField.DAY_OF_WEEK);
int month = date.get(ChronoField.MONTH_OF_YEAR);
int year = date.get(ChronoField.YEAR);
System.out.printf(" Today is %s %s %d-%s-%d%n", date.getChronology().getID(),
dow, day, month, year);
// Print today's date and the last day of the year
ChronoLocalDate now1 = Chronology.of("Hijrah").dateNow();
ChronoLocalDate first = now1.with(ChronoField.DAY_OF_MONTH, 1)
.with(ChronoField.MONTH_OF_YEAR, 1);
ChronoLocalDate last = first.plus(1, ChronoUnit.YEARS)
.minus(1, ChronoUnit.DAYS);
System.out.printf(" Today is %s: start: %s; end: %s%n", last.getChronology().getID(),
first, last);
The set of calendars is extensible by defining a subclass of ChronoLocalDate
to represent a date instance and an implementation of Chronology
to be the factory for the ChronoLocalDate subclass.
To permit the discovery of the additional calendar types the implementation of
Chronology must be registered as a Service implementing the Chronology interface
in the META-INF/Services file as per the specification of java..
The subclass must function according to the Chronology class description and must provide its
chronlogy ID and calendar type.
Implementation Specification
This abstract class 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.
| Modifier and Type | Field and Description |
|---|---|
| private static final long | serialVersionUID
Serialization version. |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| private long | |
| pack-priv static <D extends ChronoLocalDate> D | Returns: the date-time checked and cast toChronoLocalDate, not nullthe chronology to check for, not null chrono, Temporal a date-time to cast, not null temporal)Casts the |
| public boolean | equals(Object obj)
Overrides java. Implements java. Checks if this date is equal to another date, including the chronology. |
| public int | hashCode()
Overrides java. Implements java. A hash code for this date. |
| public D | minus(TemporalAmount amount)
Overrides default java. Returns an object of the same type as this object with an amount subtracted. |
| public D | minus(long amountToSubtract, TemporalUnit unit)
Overrides default java. Returns an object of the same type as this object with the specified period subtracted. |
| pack-priv D | Returns: a date based on this one with the days subtracted, not nullthe days to subtract, may be negative daysToSubtract)Returns a copy of this date with the specified number of days subtracted. |
| pack-priv D | Returns: a date based on this one with the months subtracted, not nullthe months to subtract, may be negative monthsToSubtract)Returns a copy of this date with the specified number of months subtracted. |
| pack-priv D | Returns: a date based on this one with the weeks subtracted, not nullthe weeks to subtract, may be negative weeksToSubtract)Returns a copy of this date with the specified number of weeks subtracted. |
| pack-priv D | Returns: a date based on this one with the years subtracted, not nullthe years to subtract, may be negative yearsToSubtract)Returns a copy of this date with the specified number of years subtracted. |
| private long | |
| public D | plus(TemporalAmount amount)
Overrides default java. Returns an object of the same type as this object with an amount added. |
| public D | plus(long amountToAdd, TemporalUnit unit)
Overrides default java. Implements java. Returns an object of the same type as this object with the specified period added. |
| pack-priv abstract D | Returns: a date based on this one with the days added, not nullthe days to add, may be negative daysToAdd)Returns a copy of this date with the specified number of days added. |
| pack-priv abstract D | Returns: a date based on this one with the months added, not nullthe months to add, may be negative monthsToAdd)Returns a copy of this date with the specified number of months added. |
| pack-priv D | Returns: a date based on this one with the weeks added, not nullthe weeks to add, may be negative weeksToAdd)Returns a copy of this date with the specified number of weeks added. |
| pack-priv abstract D | Returns: a date based on this one with the years added, not nullthe years to add, may be negative yearsToAdd)Returns a copy of this date with the specified number of years added. |
| public String | toString()
Overrides java. Implements java. Outputs this date as a |
| public long | until(Temporal endExclusive, TemporalUnit unit)
Implements java. Calculates the amount of time until another date in terms of the specified unit. |
| public D | with(TemporalAdjuster adjuster)
Overrides default java. Returns an adjusted object of the same type as this object with the adjustment made. |
| public D | with(TemporalField field, long value)
Overrides default java. Implements java. Returns an object of the same type as this object with the specified field altered. |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID Serialization version. | |
| ChronoLocalDateImpl | back to summary |
|---|---|
| pack-priv ChronoLocalDateImpl() Creates an instance. | |
| daysUntil | back to summary |
|---|---|
| private long daysUntil(ChronoLocalDate end) | |
| ensureValid | back to summary |
|---|---|
| pack-priv static <D extends ChronoLocalDate> D ensureValid(Chronology chrono, Temporal temporal) Casts the
| |
| equals | back to summary |
|---|---|
| public boolean equals(Object obj) Overrides java. Implements java. Doc from java. Checks if this date is equal to another date, including the chronology. Compares this date with another ensuring that the date and chronology are the same.
To compare the dates of two
| |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Implements java. Doc from java. A hash code for this date.
| |
| minus | back to summary |
|---|---|
| public D minus(TemporalAmount amount) Overrides default java. Doc from java. Returns an object of the same type as this object with an amount subtracted.
This adjusts this temporal, subtracting according to the rules of the specified amount.
The amount is typically a Some example code indicating how and why this method is used: date = date.minus(period); // subtract a Period instance date = date.minus(duration); // subtract a Duration instance date = date.minus(workingDays(6)); // example user-written workingDays method
Note that calling
| |
| minus | back to summary |
|---|---|
| public D minus(long amountToSubtract, TemporalUnit unit) Overrides default java. Doc from java. Returns an object of the same type as this object with the specified period subtracted.
This method returns a new object based on this one with the specified period subtracted.
For example, on a In some cases, changing a field is not fully defined. For example, if the target object is a date representing the 31st March, then subtracting one month would be unclear. In cases like this, the field 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.
| |
| minusDays | back to summary |
|---|---|
| pack-priv D minusDays(long daysToSubtract) Returns a copy of this date with the specified number of days subtracted. This subtracts the specified period in days to the date.
The default implementation uses This instance is immutable and unaffected by this method call.
| |
| minusMonths | back to summary |
|---|---|
| pack-priv D minusMonths(long monthsToSubtract) Returns a copy of this date with the specified number of months subtracted. This subtracts the specified period in months to the date. In some cases, subtracting months can cause the resulting date to become invalid. If this occurs, then other fields, typically the day-of-month, will be adjusted to ensure that the result is valid. Typically this will select the last valid day of the month.
The default implementation uses This instance is immutable and unaffected by this method call.
| |
| minusWeeks | back to summary |
|---|---|
| pack-priv D minusWeeks(long weeksToSubtract) Returns a copy of this date with the specified number of weeks subtracted. This subtracts the specified period in weeks to the date. In some cases, subtracting weeks can cause the resulting date to become invalid. If this occurs, then other fields will be adjusted to ensure that the result is valid.
The default implementation uses This instance is immutable and unaffected by this method call.
| |
| minusYears | back to summary |
|---|---|
| pack-priv D minusYears(long yearsToSubtract) Returns a copy of this date with the specified number of years subtracted. This subtracts the specified period in years to the date. In some cases, subtracting years can cause the resulting date to become invalid. If this occurs, then other fields, typically the day-of-month, will be adjusted to ensure that the result is valid. Typically this will select the last valid day of the month.
The default implementation uses This instance is immutable and unaffected by this method call.
| |
| monthsUntil | back to summary |
|---|---|
| private long monthsUntil(ChronoLocalDate end) | |
| plus | back to summary |
|---|---|
| public D plus(TemporalAmount amount) Overrides default java. Doc from java. Returns an object of the same type as this object with an amount added.
This adjusts this temporal, adding according to the rules of the specified amount.
The amount is typically a Some example code indicating how and why this method is used: date = date.plus(period); // add a Period instance date = date.plus(duration); // add a Duration instance date = date.plus(workingDays(6)); // example user-written workingDays method
Note that calling
| |
| plus | back to summary |
|---|---|
| public D plus(long amountToAdd, TemporalUnit unit) Overrides default java. Implements java. Doc from java. Returns an object of the same type as this object with the specified period added.
This method returns a new object based on this one with the specified period added.
For example, on a In some cases, changing a field is not fully defined. For example, if the target object is a date representing the 31st January, then adding one month would be unclear. In cases like this, the field 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.
| |
| plusDays | back to summary |
|---|---|
| pack-priv abstract D plusDays(long daysToAdd) Returns a copy of this date with the specified number of days added. This adds the specified period in days to the date. This instance is immutable and unaffected by this method call.
| |
| plusMonths | back to summary |
|---|---|
| pack-priv abstract D plusMonths(long monthsToAdd) Returns a copy of this date with the specified number of months added. This adds the specified period in months to the date. In some cases, adding months can cause the resulting date to become invalid. If this occurs, then other fields, typically the day-of-month, will be adjusted to ensure that the result is valid. Typically this will select the last valid day of the month. This instance is immutable and unaffected by this method call.
| |
| plusWeeks | back to summary |
|---|---|
| pack-priv D plusWeeks(long weeksToAdd) Returns a copy of this date with the specified number of weeks added. This adds the specified period in weeks to the date. In some cases, adding weeks can cause the resulting date to become invalid. If this occurs, then other fields will be adjusted to ensure that the result is valid.
The default implementation uses This instance is immutable and unaffected by this method call.
| |
| plusYears | back to summary |
|---|---|
| pack-priv abstract D plusYears(long yearsToAdd) Returns a copy of this date with the specified number of years added. This adds the specified period in years to the date. In some cases, adding years can cause the resulting date to become invalid. If this occurs, then other fields, typically the day-of-month, will be adjusted to ensure that the result is valid. Typically this will select the last valid day of the month. This instance is immutable and unaffected by this method call.
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Implements java. Doc from java. Outputs this date as a The output will include the full local date.
| |
| until | back to summary |
|---|---|
| public long until(Temporal endExclusive, TemporalUnit unit) Implements java. Doc from java. Calculates the amount of time until another date in terms of the specified unit.
This calculates the amount of time between two
There are two equivalent ways of using this method.
The first is to invoke this method.
The second is to use // these two lines are equivalent amount = start.until(end, MONTHS); amount = MONTHS.between(start, end);The choice should be made based on which makes the code more readable.
The calculation is implemented in this method for
If the unit is not a This instance is immutable and unaffected by this method call.
| |
| with | back to summary |
|---|---|
| public D with(TemporalAdjuster adjuster) Overrides default java. Doc from java. Returns an adjusted object of the same type as this object with the adjustment made.
This adjusts this date-time according to the rules of the specified adjuster.
A simple adjuster might simply set the one of the fields, such as the year field.
A more complex adjuster might set the date to the last day of the month.
A selection of common adjustments is provided in
Some example code indicating how and why this method is used: date = date.with(Month.JULY); // most key classes implement TemporalAdjuster date = date.with(lastDayOfMonth()); // static import from Adjusters date = date.with(next(WEDNESDAY)); // static import from Adjusters and DayOfWeek
| |
| with | back to summary |
|---|---|
| public D with(TemporalField field, long value) Overrides default java. Implements java. Doc from java. Returns an object of the same type as this object with the specified field altered.
This returns a new object based on this one with the value for the specified field changed.
For example, on a 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 field 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.
| |