implements Temporal TemporalAdjuster ChronoLocalDate Serializable
A date without a time-zone in the ISO-8601 calendar system,
such as 2007-12-03
.
LocalDate
is an immutable date-time object that represents a date,
often viewed as year-month-day. Other date fields, such as day-of-year,
day-of-week and week-of-year, can also be accessed.
For example, the value "2nd October 2007" can be stored in a LocalDate
.
This class does not store or represent a time or time-zone. Instead, it is a description of the date, as used for birthdays. It cannot represent an instant on the time-line without additional information such as an offset or time-zone.
The ISO-8601 calendar system is the modern civil calendar system used today in most of the world. It is equivalent to the proleptic Gregorian calendar system, in which today's rules for leap years are applied for all time. For most applications written today, the ISO-8601 rules are entirely suitable. However, any application that makes use of historical dates, and requires them to be accurate will find the ISO-8601 approach unsuitable.
Field Summary
public static final LocalDate | MAX | The maximum supported LocalDate , '+999999999-12-31'. |
public static final LocalDate | MIN | The minimum supported LocalDate , '-999999999-01-01'. |
Public Method Summary
Temporal |
adjustInto(Temporal temporal)
Adjusts the specified temporal object to have the same date as this object.
|
ZonedDateTime |
atStartOfDay(ZoneId zone)
Returns a zoned date-time from this date at the earliest valid time according
to the rules in the time-zone.
|
LocalDateTime |
atStartOfDay()
Combines this date with the time of midnight to create a
LocalDateTime
at the start of this date. |
OffsetDateTime | |
LocalDateTime |
atTime(int hour, int minute)
Combines this date with a time to create a
LocalDateTime . |
LocalDateTime |
atTime(int hour, int minute, int second)
Combines this date with a time to create a
LocalDateTime . |
LocalDateTime |
atTime(int hour, int minute, int second, int nanoOfSecond)
Combines this date with a time to create a
LocalDateTime . |
LocalDateTime | |
int | |
boolean | |
String | |
static LocalDate | |
int | |
IsoChronology |
getChronology()
Gets the chronology of this date, which is the ISO calendar system.
|
int |
getDayOfMonth()
Gets the day-of-month field.
|
DayOfWeek |
getDayOfWeek()
Gets the day-of-week field, which is an enum
DayOfWeek . |
int |
getDayOfYear()
Gets the day-of-year field.
|
Era |
getEra()
Gets the era applicable at this date.
|
long | |
Month |
getMonth()
Gets the month-of-year field using the
Month enum. |
int |
getMonthValue()
Gets the month-of-year field from 1 to 12.
|
int |
getYear()
Gets the year field.
|
int |
hashCode()
A hash code for this date.
|
boolean | |
boolean | |
boolean | |
boolean |
isLeapYear()
Checks if the year is a leap year, according to the ISO proleptic
calendar system rules.
|
boolean | |
boolean | |
int |
lengthOfMonth()
Returns the length of the month represented by this date.
|
int |
lengthOfYear()
Returns the length of the year represented by this date.
|
LocalDate |
minus(TemporalAmount amountToSubtract)
Returns a copy of this date with the specified amount subtracted.
|
LocalDate |
minus(long amountToSubtract, TemporalUnit unit)
Returns a copy of this date with the specified amount subtracted.
|
LocalDate |
minusDays(long daysToSubtract)
Returns a copy of this
LocalDate with the specified number of days subtracted. |
LocalDate |
minusMonths(long monthsToSubtract)
Returns a copy of this
LocalDate with the specified number of months subtracted. |
LocalDate |
minusWeeks(long weeksToSubtract)
Returns a copy of this
LocalDate with the specified number of weeks subtracted. |
LocalDate |
minusYears(long yearsToSubtract)
Returns a copy of this
LocalDate with the specified number of years subtracted. |
static LocalDate | |
static LocalDate |
now()
Obtains the current date from the system clock in the default time-zone.
|
static LocalDate | |
static LocalDate |
of(int year, int month, int dayOfMonth)
Obtains an instance of
LocalDate from a year, month and day. |
static LocalDate | |
static LocalDate |
ofEpochDay(long epochDay)
Obtains an instance of
LocalDate from the epoch day count. |
static LocalDate |
ofYearDay(int year, int dayOfYear)
Obtains an instance of
LocalDate from a year and day-of-year. |
static LocalDate |
parse(CharSequence text, DateTimeFormatter formatter)
Obtains an instance of
LocalDate from a text string using a specific formatter. |
static LocalDate | |
LocalDate | |
LocalDate |
plus(long amountToAdd, TemporalUnit unit)
Returns a copy of this date with the specified amount added.
|
LocalDate |
plusDays(long daysToAdd)
Returns a copy of this
LocalDate with the specified number of days added. |
LocalDate |
plusMonths(long monthsToAdd)
Returns a copy of this
LocalDate with the specified number of months added. |
LocalDate |
plusWeeks(long weeksToAdd)
Returns a copy of this
LocalDate with the specified number of weeks added. |
LocalDate |
plusYears(long yearsToAdd)
Returns a copy of this
LocalDate with the specified number of years added. |
<R> R | |
ValueRange | |
long |
toEpochDay()
Converts this date to the Epoch Day.
|
String |
toString()
Outputs this date as a
String , such as 2007-12-03 . |
long |
until(Temporal endExclusive, TemporalUnit unit)
Calculates the amount of time until another date in terms of the specified unit.
|
Period |
until(ChronoLocalDate endDateExclusive)
Calculates the period between this date and another date as a
Period . |
LocalDate |
with(TemporalField field, long newValue)
Returns a copy of this date with the specified field set to a new value.
|
LocalDate | |
LocalDate |
withDayOfMonth(int dayOfMonth)
Returns a copy of this
LocalDate with the day-of-month altered. |
LocalDate |
withDayOfYear(int dayOfYear)
Returns a copy of this
LocalDate with the day-of-year altered. |
LocalDate |
withMonth(int month)
Returns a copy of this
LocalDate with the month-of-year altered. |
LocalDate |
withYear(int year)
Returns a copy of this
LocalDate with the year altered. |
Inherited Method Summary
Fields
public static final LocalDate MAX
The maximum supported LocalDate
, '+999999999-12-31'.
This could be used by an application as a "far future" date.
public static final LocalDate MIN
The minimum supported LocalDate
, '-999999999-01-01'.
This could be used by an application as a "far past" date.
Public Methods
public Temporal adjustInto (Temporal temporal)
Adjusts the specified temporal object to have the same date as this object.
This returns a temporal object of the same observable type as the input with the date changed to be the same as this.
The adjustment is equivalent to using Temporal.with(TemporalField, long)
passing ChronoField.EPOCH_DAY
as the field.
In most cases, it is clearer to reverse the calling pattern by using
Temporal.with(TemporalAdjuster)
:
// these two lines are equivalent, but the second approach is recommended temporal = thisLocalDate.adjustInto(temporal); temporal = temporal.with(thisLocalDate);
This instance is immutable and unaffected by this method call.
Parameters
temporal | the target object to be adjusted, not null |
---|
Returns
- the adjusted object, not null
Throws
DateTimeException | if unable to make the adjustment |
---|---|
ArithmeticException | if numeric overflow occurs |
public ZonedDateTime atStartOfDay (ZoneId zone)
Returns a zoned date-time from this date at the earliest valid time according to the rules in the time-zone.
Time-zone rules, such as daylight savings, mean that not every local date-time is valid for the specified zone, thus the local date-time may not be midnight.
In most cases, there is only one valid offset for a local date-time. In the case of an overlap, there are two valid offsets, and the earlier one is used, corresponding to the first occurrence of midnight on the date. In the case of a gap, the zoned date-time will represent the instant just after the gap.
If the zone ID is a ZoneOffset
, then the result always has a time of midnight.
To convert to a specific time in a given time-zone call atTime(LocalTime)
followed by LocalDateTime.atZone(ZoneId)
.
Parameters
zone | the zone ID to use, not null |
---|
Returns
- the zoned date-time formed from this date and the earliest valid time for the zone, not null
public LocalDateTime atStartOfDay ()
Combines this date with the time of midnight to create a LocalDateTime
at the start of this date.
This returns a LocalDateTime
formed from this date at the time of
midnight, 00:00, at the start of this date.
Returns
- the local date-time of midnight at the start of this date, not null
public OffsetDateTime atTime (OffsetTime time)
Combines this date with an offset time to create an OffsetDateTime
.
This returns an OffsetDateTime
formed from this date at the specified time.
All possible combinations of date and time are valid.
Parameters
time | the time to combine with, not null |
---|
Returns
- the offset date-time formed from this date and the specified time, not null
public LocalDateTime atTime (int hour, int minute)
Combines this date with a time to create a LocalDateTime
.
This returns a LocalDateTime
formed from this date at the
specified hour and minute.
The seconds and nanosecond fields will be set to zero.
The individual time fields must be within their valid range.
All possible combinations of date and time are valid.
Parameters
hour | the hour-of-day to use, from 0 to 23 |
---|---|
minute | the minute-of-hour to use, from 0 to 59 |
Returns
- the local date-time formed from this date and the specified time, not null
Throws
DateTimeException | if the value of any field is out of range |
---|
public LocalDateTime atTime (int hour, int minute, int second)
Combines this date with a time to create a LocalDateTime
.
This returns a LocalDateTime
formed from this date at the
specified hour, minute and second.
The nanosecond field will be set to zero.
The individual time fields must be within their valid range.
All possible combinations of date and time are valid.
Parameters
hour | the hour-of-day to use, from 0 to 23 |
---|---|
minute | the minute-of-hour to use, from 0 to 59 |
second | the second-of-minute to represent, from 0 to 59 |
Returns
- the local date-time formed from this date and the specified time, not null
Throws
DateTimeException | if the value of any field is out of range |
---|
public LocalDateTime atTime (int hour, int minute, int second, int nanoOfSecond)
Combines this date with a time to create a LocalDateTime
.
This returns a LocalDateTime
formed from this date at the
specified hour, minute, second and nanosecond.
The individual time fields must be within their valid range.
All possible combinations of date and time are valid.
Parameters
hour | the hour-of-day to use, from 0 to 23 |
---|---|
minute | the minute-of-hour to use, from 0 to 59 |
second | the second-of-minute to represent, from 0 to 59 |
nanoOfSecond | the nano-of-second to represent, from 0 to 999,999,999 |
Returns
- the local date-time formed from this date and the specified time, not null
Throws
DateTimeException | if the value of any field is out of range |
---|
public LocalDateTime atTime (LocalTime time)
Combines this date with a time to create a LocalDateTime
.
This returns a LocalDateTime
formed from this date at the specified time.
All possible combinations of date and time are valid.
Parameters
time | the time to combine with, not null |
---|
Returns
- the local date-time formed from this date and the specified time, not null
public int compareTo (ChronoLocalDate other)
Compares this date to another date.
The comparison is primarily based on the date, from earliest to latest.
It is "consistent with equals", as defined by Comparable
.
If all the dates being compared are instances of LocalDate
,
then the comparison will be entirely based on the date.
If some dates being compared are in different chronologies, then the
chronology is also considered, see Comparable.compareTo(T)
.
Parameters
other | the other date to compare to, not null |
---|
Returns
- the comparator value, negative if less, positive if greater
public boolean equals (Object obj)
Checks if this date is equal to another date.
Compares this LocalDate
with another ensuring that the date is the same.
Only objects of type LocalDate
are compared, other types return false.
To compare the dates of two TemporalAccessor
instances, including dates
in two different chronologies, use ChronoField.EPOCH_DAY
as a comparator.
Parameters
obj | the object to check, null returns false |
---|
Returns
- true if this is equal to the other date
public String format (DateTimeFormatter formatter)
Formats this date using the specified formatter.
This date will be passed to the formatter to produce a string.
Parameters
formatter | the formatter to use, not null |
---|
Returns
- the formatted date string, not null
Throws
DateTimeException | if an error occurs during printing |
---|
public static LocalDate from (TemporalAccessor temporal)
Obtains an instance of LocalDate
from a temporal object.
This obtains a local date based on the specified temporal.
A TemporalAccessor
represents an arbitrary set of date and time information,
which this factory converts to an instance of LocalDate
.
The conversion uses the TemporalQueries.localDate()
query, which relies
on extracting the EPOCH_DAY
field.
This method matches the signature of the functional interface TemporalQuery
allowing it to be used as a query via method reference, LocalDate::from
.
Parameters
temporal | the temporal object to convert, not null |
---|
Returns
- the local date, not null
Throws
DateTimeException | if unable to convert to a LocalDate
|
---|
public int get (TemporalField field)
Gets the value of the specified field from this date as an int
.
This queries this date for the value of the specified field. The returned value will always be within the valid range of values for the field. If it is not possible to return the value, because the field is not supported or for some other reason, an exception is thrown.
If the field is a ChronoField
then the query is implemented here.
The supported fields
will return valid
values based on this date, except EPOCH_DAY
and PROLEPTIC_MONTH
which are too large to fit in an int
and throw a DateTimeException
.
All other ChronoField
instances will throw an UnsupportedTemporalTypeException
.
If the field is not a ChronoField
, then the result of this method
is obtained by invoking TemporalField.getFrom(TemporalAccessor)
passing this
as the argument. Whether the value can be obtained,
and what the value represents, is determined by the field.
Parameters
field | the field to get, not null |
---|
Returns
- the value for the field
Throws
DateTimeException | if a value for the field cannot be obtained or the value is outside the range of valid values for the field |
---|---|
UnsupportedTemporalTypeException | if the field is not supported or
the range of values exceeds an int |
ArithmeticException | if numeric overflow occurs |
public IsoChronology getChronology ()
Gets the chronology of this date, which is the ISO calendar system.
The Chronology
represents the calendar system in use.
The ISO-8601 calendar system is the modern civil calendar system used today
in most of the world. It is equivalent to the proleptic Gregorian calendar
system, in which today's rules for leap years are applied for all time.
Returns
- the ISO chronology, not null
public int getDayOfMonth ()
Gets the day-of-month field.
This method returns the primitive int
value for the day-of-month.
Returns
- the day-of-month, from 1 to 31
public DayOfWeek getDayOfWeek ()
Gets the day-of-week field, which is an enum DayOfWeek
.
This method returns the enum DayOfWeek
for the day-of-week.
This avoids confusion as to what int
values mean.
If you need access to the primitive int
value then the enum
provides the int value
.
Additional information can be obtained from the DayOfWeek
.
This includes textual names of the values.
Returns
- the day-of-week, not null
public int getDayOfYear ()
Gets the day-of-year field.
This method returns the primitive int
value for the day-of-year.
Returns
- the day-of-year, from 1 to 365, or 366 in a leap year
public Era getEra ()
Gets the era applicable at this date.
The official ISO-8601 standard does not define eras, however IsoChronology
does.
It defines two eras, 'CE' from year one onwards and 'BCE' from year zero backwards.
Since dates before the Julian-Gregorian cutover are not in line with history,
the cutover between 'BCE' and 'CE' is also not aligned with the commonly used
eras, often referred to using 'BC' and 'AD'.
Users of this class should typically ignore this method as it exists primarily
to fulfill the ChronoLocalDate
contract where it is necessary to support
the Japanese calendar system.
The returned era will be a singleton capable of being compared with the constants
in IsoChronology
using the ==
operator.
Returns
- the
IsoChronology
era constant applicable at this date, not null
public long getLong (TemporalField field)
Gets the value of the specified field from this date as a long
.
This queries this date for the value of the specified field. If it is not possible to return the value, because the field is not supported or for some other reason, an exception is thrown.
If the field is a ChronoField
then the query is implemented here.
The supported fields
will return valid
values based on this date.
All other ChronoField
instances will throw an UnsupportedTemporalTypeException
.
If the field is not a ChronoField
, then the result of this method
is obtained by invoking TemporalField.getFrom(TemporalAccessor)
passing this
as the argument. Whether the value can be obtained,
and what the value represents, is determined by the field.
Parameters
field | the field to get, not null |
---|
Returns
- the value for the field
Throws
DateTimeException | if a value for the field cannot be obtained |
---|---|
UnsupportedTemporalTypeException | if the field is not supported |
ArithmeticException | if numeric overflow occurs |
public Month getMonth ()
public int getMonthValue ()
Gets the month-of-year field from 1 to 12.
This method returns the month as an int
from 1 to 12.
Application code is frequently clearer if the enum Month
is used by calling getMonth()
.
Returns
- the month-of-year, from 1 to 12
See Also
public int getYear ()
Gets the year field.
This method returns the primitive int
value for the year.
The year returned by this method is proleptic as per get(YEAR)
.
To obtain the year-of-era, use get(YEAR_OF_ERA)
.
Returns
- the year, from MIN_YEAR to MAX_YEAR
public int hashCode ()
A hash code for this date.
Returns
- a suitable hash code
public boolean isAfter (ChronoLocalDate other)
Checks if this date is after the specified date.
This checks to see if this date represents a point on the local time-line after the other date.
LocalDate a = LocalDate.of(2012, 6, 30); LocalDate b = LocalDate.of(2012, 7, 1); a.isAfter(b) == false a.isAfter(a) == false b.isAfter(a) == true
This method only considers the position of the two dates on the local time-line.
It does not take into account the chronology, or calendar system.
This is different from the comparison in compareTo(ChronoLocalDate)
,
but is the same approach as ChronoLocalDate.timeLineOrder()
.
Parameters
other | the other date to compare to, not null |
---|
Returns
- true if this date is after the specified date
public boolean isBefore (ChronoLocalDate other)
Checks if this date is before the specified date.
This checks to see if this date represents a point on the local time-line before the other date.
LocalDate a = LocalDate.of(2012, 6, 30); LocalDate b = LocalDate.of(2012, 7, 1); a.isBefore(b) == true a.isBefore(a) == false b.isBefore(a) == false
This method only considers the position of the two dates on the local time-line.
It does not take into account the chronology, or calendar system.
This is different from the comparison in compareTo(ChronoLocalDate)
,
but is the same approach as ChronoLocalDate.timeLineOrder()
.
Parameters
other | the other date to compare to, not null |
---|
Returns
- true if this date is before the specified date
public boolean isEqual (ChronoLocalDate other)
Checks if this date is equal to the specified date.
This checks to see if this date represents the same point on the local time-line as the other date.
LocalDate a = LocalDate.of(2012, 6, 30); LocalDate b = LocalDate.of(2012, 7, 1); a.isEqual(b) == false a.isEqual(a) == true b.isEqual(a) == false
This method only considers the position of the two dates on the local time-line.
It does not take into account the chronology, or calendar system.
This is different from the comparison in compareTo(ChronoLocalDate)
but is the same approach as ChronoLocalDate.timeLineOrder()
.
Parameters
other | the other date to compare to, not null |
---|
Returns
- true if this date is equal to the specified date
public boolean isLeapYear ()
Checks if the year is a leap year, according to the ISO proleptic calendar system rules.
This method applies the current rules for leap years across the whole time-line. In general, a year is a leap year if it is divisible by four without remainder. However, years divisible by 100, are not leap years, with the exception of years divisible by 400 which are.
For example, 1904 is a leap year it is divisible by 4. 1900 was not a leap year as it is divisible by 100, however 2000 was a leap year as it is divisible by 400.
The calculation is proleptic - applying the same rules into the far future and far past. This is historically inaccurate, but is correct for the ISO-8601 standard.
Returns
- true if the year is leap, false otherwise
public boolean isSupported (TemporalUnit unit)
Checks if the specified unit is supported.
This checks if the specified unit can be added to, or subtracted from, this date.
If false, then calling the plus(long, TemporalUnit)
and
minus
methods will throw an exception.
If the unit is a ChronoUnit
then the query is implemented here.
The supported units are:
DAYS
WEEKS
MONTHS
YEARS
DECADES
CENTURIES
MILLENNIA
ERAS
ChronoUnit
instances will return false.
If the unit is not a ChronoUnit
, then the result of this method
is obtained by invoking TemporalUnit.isSupportedBy(Temporal)
passing this
as the argument.
Whether the unit is supported is determined by the unit.
Parameters
unit | the unit to check, null returns false |
---|
Returns
- true if the unit can be added/subtracted, false if not
public boolean isSupported (TemporalField field)
Checks if the specified field is supported.
This checks if this date can be queried for the specified field.
If false, then calling the range
,
get
and with(TemporalField, long)
methods will throw an exception.
If the field is a ChronoField
then the query is implemented here.
The supported fields are:
DAY_OF_WEEK
ALIGNED_DAY_OF_WEEK_IN_MONTH
ALIGNED_DAY_OF_WEEK_IN_YEAR
DAY_OF_MONTH
DAY_OF_YEAR
EPOCH_DAY
ALIGNED_WEEK_OF_MONTH
ALIGNED_WEEK_OF_YEAR
MONTH_OF_YEAR
PROLEPTIC_MONTH
YEAR_OF_ERA
YEAR
ERA
ChronoField
instances will return false.
If the field is not a ChronoField
, then the result of this method
is obtained by invoking TemporalField.isSupportedBy(TemporalAccessor)
passing this
as the argument.
Whether the field is supported is determined by the field.
Parameters
field | the field to check, null returns false |
---|
Returns
- true if the field is supported on this date, false if not
public int lengthOfMonth ()
Returns the length of the month represented by this date.
This returns the length of the month in days. For example, a date in January would return 31.
Returns
- the length of the month in days