Class DayOfYear
- All Implemented Interfaces:
Serializable,Comparable<DayOfYear>,TemporalAccessor,TemporalAdjuster
DayOfYear is an immutable date-time object that represents a day-of-year.
It is a type-safe way of representing a day-of-year in an application.
Any field that can be derived from a day-of-year can be obtained.
This class does not store or represent a year, month, time or time-zone.
For example, the value "51" can be stored in a DayOfYear and
would represent the 51st day of any year.
Implementation Requirements:
This class is immutable and thread-safe.This class must be treated as a value type. Do not synchronize, rely on the identity hash code or use the distinction between equals() and ==.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionadjustInto(Temporal temporal) Adjusts the specified temporal object to have this day-of-year.atYear(int year) Combines this day-of-year with a year to create aLocalDate.Combines this day-of-year with a year to create aLocalDate.intCompares this day-of-year to another.booleanChecks if this day-of-year is equal to another day-of-year.static DayOfYearfrom(TemporalAccessor temporal) Obtains an instance ofDayOfYearfrom a date-time object.intget(TemporalField field) Gets the value of the specified field from this day-of-year as anint.longgetLong(TemporalField field) Gets the value of the specified field from this day-of-year as along.intgetValue()Gets the day-of-year value.inthashCode()A hash code for this day-of-year.booleanisSupported(TemporalField field) Checks if the specified field is supported.booleanisValidYear(int year) Checks if the year is valid for this day-of-year.static DayOfYearnow()Obtains the current day-of-year from the system clock in the default time-zone.static DayOfYearObtains the current day-of-year from the specified clock.static DayOfYearObtains the current day-of-year from the system clock in the specified time-zone.static DayOfYearof(int dayOfYear) Obtains an instance ofDayOfYear.<R> Rquery(TemporalQuery<R> query) Queries this day-of-year using the specified query.range(TemporalField field) Gets the range of valid values for the specified field.toString()Outputs this day-of-year as aString.
-
Method Details
-
now
Obtains the current day-of-year from the system clock in the default time-zone.This will query the
system clockin the default time-zone to obtain the current day-of-year. The zone and offset will be set based on the time-zone in the clock.Using this method will prevent the ability to use an alternate clock for testing because the clock is hard-coded.
- Returns:
- the current day-of-year using the system clock and default time-zone, not null
-
now
Obtains the current day-of-year from the system clock in the specified time-zone.This will query the
system clockto obtain the current day-of-year. 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.
- Parameters:
zone- the zone ID to use, not null- Returns:
- the current day-of-year using the system clock, not null
-
now
Obtains the current day-of-year from the specified clock.This will query the specified clock to obtain the current day-of-year. Using this method allows the use of an alternate clock for testing. The alternate clock may be introduced using
dependency injection.- Parameters:
clock- the clock to use, not null- Returns:
- the current day-of-year, not null
-
of
Obtains an instance ofDayOfYear.A day-of-year object represents one of the 366 days of the year, from 1 to 366.
- Parameters:
dayOfYear- the day-of-year to represent, from 1 to 366- Returns:
- the day-of-year, not null
- Throws:
DateTimeException- if the day-of-year is invalid
-
from
Obtains an instance ofDayOfYearfrom a date-time object.This obtains a day-of-year based on the specified temporal. A
TemporalAccessorrepresents an arbitrary set of date and time information, which this factory converts to an instance ofDayOfYear.The conversion extracts the
day-of-yearfield. The extraction is only permitted if the temporal object has an ISO chronology, or can be converted to aLocalDate.This method matches the signature of the functional interface
TemporalQueryallowing it to be used in queries via method reference,DayOfYear::from.- Parameters:
temporal- the temporal object to convert, not null- Returns:
- the day-of-year, not null
- Throws:
DateTimeException- if unable to convert to aDayOfYear
-
getValue
public int getValue()Gets the day-of-year value.- Returns:
- the day-of-year, from 1 to 366
-
isSupported
Checks if the specified field is supported.This checks if this day-of-year can be queried for the specified field. If false, then calling the
range,getandgetLongmethods will throw an exception.If the field is a
ChronoFieldthen the query is implemented here. The supported fields are:DAY_OF_YEAR
ChronoFieldinstances will return false.If the field is not a
ChronoField, then the result of this method is obtained by invokingTemporalField.isSupportedBy(TemporalAccessor)passingthisas the argument. Whether the field is supported is determined by the field.- Specified by:
isSupportedin interfaceTemporalAccessor- Parameters:
field- the field to check, null returns false- Returns:
- true if the field is supported on this day-of-year, false if not
-
range
Gets the range of valid values for the specified field.The range object expresses the minimum and maximum valid values for a field. This day-of-year is used to enhance the accuracy of the returned range. If it is not possible to return the range, because the field is not supported or for some other reason, an exception is thrown.
If the field is a
ChronoFieldthen the query is implemented here. Thesupported fieldswill return appropriate range instances. All otherChronoFieldinstances will throw anUnsupportedTemporalTypeException.If the field is not a
ChronoField, then the result of this method is obtained by invokingTemporalField.rangeRefinedBy(TemporalAccessor)passingthisas the argument. Whether the range can be obtained is determined by the field.- Specified by:
rangein interfaceTemporalAccessor- Parameters:
field- the field to query 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 obtainedUnsupportedTemporalTypeException- if the field is not supported
-
get
Gets the value of the specified field from this day-of-year as anint.This queries this day-of-year for the value for 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
ChronoFieldthen the query is implemented here. Thesupported fieldswill return valid values based on this day-of-year. All otherChronoFieldinstances will throw anUnsupportedTemporalTypeException.If the field is not a
ChronoField, then the result of this method is obtained by invokingTemporalField.getFrom(TemporalAccessor)passingthisas the argument. Whether the value can be obtained, and what the value represents, is determined by the field.- Specified by:
getin interfaceTemporalAccessor- 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 fieldUnsupportedTemporalTypeException- if the field is not supported or the range of values exceeds anintArithmeticException- if numeric overflow occurs
-
getLong
Gets the value of the specified field from this day-of-year as along.This queries this day-of-year for the value for 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
ChronoFieldthen the query is implemented here. Thesupported fieldswill return valid values based on this day-of-year. All otherChronoFieldinstances will throw anUnsupportedTemporalTypeException.If the field is not a
ChronoField, then the result of this method is obtained by invokingTemporalField.getFrom(TemporalAccessor)passingthisas the argument. Whether the value can be obtained, and what the value represents, is determined by the field.- Specified by:
getLongin interfaceTemporalAccessor- Parameters:
field- the field to get, not null- Returns:
- the value for the field
- Throws:
DateTimeException- if a value for the field cannot be obtainedUnsupportedTemporalTypeException- if the field is not supportedArithmeticException- if numeric overflow occurs
-
isValidYear
public boolean isValidYear(int year) Checks if the year is valid for this day-of-year.This method checks whether this day-of-year and the input year form a valid date. This can only return false for day-of-year 366.
- Parameters:
year- the year to validate- Returns:
- true if the year is valid for this day-of-year
-
query
Queries this day-of-year using the specified query.TemporalQueries.chronology()andTemporalQueries.precision()are directly supported. Otherwise, the result of this method is obtained by invokingTemporalAccessor.query(TemporalQuery)on the parent interface.- Specified by:
queryin interfaceTemporalAccessor- Type Parameters:
R- the type of the result- Parameters:
query- the query to invoke, not null- Returns:
- the query result, null may be returned (defined by the query)
- Throws:
DateTimeException- if unable to query (defined by the query)ArithmeticException- if numeric overflow occurs (defined by the query)
-
adjustInto
Adjusts the specified temporal object to have this day-of-year.This returns a temporal object of the same observable type as the input with the day-of-year changed to be the same as this.
The adjustment is equivalent to using
Temporal.with(TemporalField, long)passingChronoField.DAY_OF_YEARas the field. If the specified temporal object does not use the ISO calendar system then aDateTimeExceptionis thrown.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 = thisDay.adjustInto(temporal); temporal = temporal.with(thisDay);
This instance is immutable and unaffected by this method call.
- Specified by:
adjustIntoin interfaceTemporalAdjuster- Parameters:
temporal- the target object to be adjusted, not null- Returns:
- the adjusted object, not null
- Throws:
DateTimeException- if unable to make the adjustmentArithmeticException- if numeric overflow occurs
-
atYear
Combines this day-of-year with a year to create aLocalDate.This returns a
LocalDateformed from this day and the specified year.This method can be used as part of a chain to produce a date:
LocalDate date = day.atYear(year);
The day-of-year value 366 is only valid in a leap year.
- Parameters:
year- the year to use, not null- Returns:
- the local date formed from this day and the specified year, not null
- Throws:
DateTimeException- if the year is invalid or this is day 366 and the year is not a leap year
-
atYear
Combines this day-of-year with a year to create aLocalDate.This returns a
LocalDateformed from this day and the specified year.This method can be used as part of a chain to produce a date:
LocalDate date = day.atYear(year);
The day-of-year value 366 is only valid in a leap year.
- Parameters:
year- the year to use, from MIN_YEAR to MAX_YEAR- Returns:
- the local date formed from this day and the specified year, not null
- Throws:
DateTimeException- if the year is invalid or this is day 366 and the year is not a leap year
-
compareTo
Compares this day-of-year to another.The comparison is based on the value of the day. It is "consistent with equals", as defined by
Comparable.- Specified by:
compareToin interfaceComparable<DayOfYear>- Parameters:
other- the other day-of-year instance, not null- Returns:
- the comparator value, negative if less, positive if greater
-
equals
Checks if this day-of-year is equal to another day-of-year. -
hashCode
public int hashCode()A hash code for this day-of-year. -
toString
Outputs this day-of-year as aString.
-