public final class YearWeek extends Object implements TemporalAccessor, TemporalAdjuster, Comparable<YearWeek>, Serializable
2015-W13
YearWeek
is an immutable date-time object that represents the combination
of a week-based-year and week-of-week-based-year.
Any field that can be derived from those two fields can be obtained.
This class does not store or represent a day, time or time-zone.
For example, the value "13th week of 2007" can be stored in a YearWeek
.
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.
ISO-8601 defines the week as always starting with Monday. The first week is the week which contains the first Thursday of the calendar year. As such, the week-based-year used in this class does not align with the calendar year.
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 ==.
Modifier and Type | Method and Description |
---|---|
Temporal |
adjustInto(Temporal temporal)
Adjusts the specified temporal object to have this year-week.
|
LocalDate |
atDay(DayOfWeek dayOfWeek)
Combines this year-week with a day-of-week to create a
LocalDate . |
int |
compareTo(YearWeek other)
Compares this year-week to another
|
boolean |
equals(Object obj)
Checks if this year-week is equal to another year-week.
|
String |
format(DateTimeFormatter formatter)
Formats this year-week using the specified formatter.
|
static YearWeek |
from(TemporalAccessor temporal)
Obtains an instance of
YearWeek from a temporal object. |
int |
get(TemporalField field)
Gets the value of the specified field from this year-week as an
int . |
long |
getLong(TemporalField field)
Gets the value of the specified field from this year-week as a
long . |
int |
getWeek()
Gets the week-of-week-based-year field.
|
int |
getYear()
Gets the week-based-year field.
|
int |
hashCode()
A hash code for this year-week.
|
boolean |
is53WeekYear()
Checks if the week-based-year has 53 weeks.
|
boolean |
isAfter(YearWeek other)
Is this year-week after the specified year-week.
|
boolean |
isBefore(YearWeek other)
Is this year-week before the specified year-week.
|
boolean |
isSupported(TemporalField field)
Checks if the specified field is supported.
|
int |
lengthOfYear()
Returns the length of the week-based-year.
|
YearWeek |
minusWeeks(long weeksToSubtract)
Returns a copy of this year-week with the specified number of weeks subtracted.
|
YearWeek |
minusYears(long yearsToSubtract)
Returns a copy of this year-week with the specified number of years subtracted.
|
static YearWeek |
now()
Obtains the current year-week from the system clock in the default time-zone.
|
static YearWeek |
now(Clock clock)
Obtains the current year-week from the specified clock.
|
static YearWeek |
now(ZoneId zone)
Obtains the current year-week from the system clock in the specified time-zone.
|
static YearWeek |
of(int weekBasedYear,
int week)
Obtains an instance of
YearWeek from a week-based-year and week. |
static YearWeek |
parse(CharSequence text)
Obtains an instance of
YearWeek from a text string such as 2007-W13 . |
static YearWeek |
parse(CharSequence text,
DateTimeFormatter formatter)
Obtains an instance of
YearWeek from a text string using a specific formatter. |
YearWeek |
plusWeeks(long weeksToAdd)
Returns a copy of this year-week with the specified number of weeks added.
|
YearWeek |
plusYears(long yearsToAdd)
Returns a copy of this year-week with the specified number of years added.
|
<R> R |
query(TemporalQuery<R> query)
Queries this year-week using the specified query.
|
ValueRange |
range(TemporalField field)
Gets the range of valid values for the specified field.
|
String |
toString()
Outputs this year-week as a
String , such as 2015-W13 . |
YearWeek |
withWeek(int week)
Returns a copy of this
YearWeek with the week altered. |
YearWeek |
withYear(int weekBasedYear)
Returns a copy of this
YearWeek with the week-based-year altered. |
public static YearWeek now()
This will query the system clock
in the default
time-zone to obtain the current year-week.
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.
public static YearWeek now(ZoneId zone)
This will query the system clock
to obtain the current year-week.
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 nullpublic static YearWeek now(Clock clock)
This will query the specified clock to obtain the current year-week.
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 nullpublic static YearWeek of(int weekBasedYear, int week)
YearWeek
from a week-based-year and week.
If the week is 53 and the year does not have 53 weeks, week one of the following year is selected.
weekBasedYear
- the week-based-year to represent, from MIN_YEAR to MAX_YEARweek
- the week-of-week-based-year to represent, from 1 to 53DateTimeException
- if either field is invalidpublic static YearWeek from(TemporalAccessor temporal)
YearWeek
from a temporal object.
This obtains a year-week based on the specified temporal.
A TemporalAccessor
represents an arbitrary set of date and time information,
which this factory converts to an instance of YearWeek
.
The conversion extracts the WEEK_BASED_YEAR
and
WEEK_OF_WEEK_BASED_YEAR
fields.
The extraction is only permitted if the temporal object has an ISO
chronology, or can be converted to a LocalDate
.
This method matches the signature of the functional interface TemporalQuery
allowing it to be used in queries via method reference, YearWeek::from
.
temporal
- the temporal object to convert, not nullDateTimeException
- if unable to convert to a YearWeek
public static YearWeek parse(CharSequence text)
YearWeek
from a text string such as 2007-W13
.
The string must represent a valid year-week.
Week 53 will be adjusted to week 1 of the following year if necessary.
The format must be YYYY-'W'ww
.
Years outside the range 0000 to 9999 must be prefixed by the plus or minus symbol.
text
- the text to parse such as "2007-W13", not nullDateTimeParseException
- if the text cannot be parsedpublic static YearWeek parse(CharSequence text, DateTimeFormatter formatter)
YearWeek
from a text string using a specific formatter.
The text is parsed using the formatter, returning a year-week.
text
- the text to parse, not nullformatter
- the formatter to use, not nullDateTimeParseException
- if the text cannot be parsedpublic boolean isSupported(TemporalField field)
This checks if this year-week can be queried for the specified field.
If false, then calling the range
and
get
methods will throw an exception.
If the field is a ChronoField
then the query is implemented here.
The supported fields are:
WEEK_OF_WEEK_BASED_YEAR
WEEK_BASED_YEAR
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.
isSupported
in interface TemporalAccessor
field
- the field to check, null returns falsepublic ValueRange range(TemporalField field)
The range object expresses the minimum and maximum valid values for a field. This year-week 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.
The range for the WEEK_BASED_YEAR
and
WEEK_OF_WEEK_BASED_YEAR
fields is returned.
All ChronoField
instances will throw an UnsupportedTemporalTypeException
.
range
in interface TemporalAccessor
field
- the field to query the range for, not nullDateTimeException
- if the range for the field cannot be obtainedUnsupportedTemporalTypeException
- if the field is not supportedpublic int get(TemporalField field)
int
.
This queries this year-week 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.
The value for the WEEK_BASED_YEAR
and
WEEK_OF_WEEK_BASED_YEAR
fields is returned.
All ChronoField
instances will throw an UnsupportedTemporalTypeException
.
get
in interface TemporalAccessor
field
- the field to get, not nullDateTimeException
- 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 an int
ArithmeticException
- if numeric overflow occurspublic long getLong(TemporalField field)
long
.
This queries this year-week 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.
The value for the WEEK_BASED_YEAR
and
WEEK_OF_WEEK_BASED_YEAR
fields is returned.
All ChronoField
instances will throw an UnsupportedTemporalTypeException
.
getLong
in interface TemporalAccessor
field
- the field to get, not nullDateTimeException
- if a value for the field cannot be obtainedUnsupportedTemporalTypeException
- if the field is not supportedArithmeticException
- if numeric overflow occurspublic int getYear()
This method returns the primitive int
value for the week-based-year.
Note that the ISO week-based-year does not align with the standard Gregorian/ISO calendar year.
public int getWeek()
This method returns the primitive int
value for the week of the week-based-year.
public boolean is53WeekYear()
This determines if the year has 53 weeks, returning true. If false, the year has 52 weeks.
public int lengthOfYear()
This returns the length of the year in days, either 364 or 371.
public YearWeek withYear(int weekBasedYear)
YearWeek
with the week-based-year altered.
This returns a year-week with the specified week-based-year. If the week of this instance is 53 and the new year does not have 53 weeks, the week will be adjusted to be 52.
This instance is immutable and unaffected by this method call.
weekBasedYear
- the week-based-year to set in the returned year-weekYearWeek
based on this year-week with the requested year, not nullDateTimeException
- if the week-based-year value is invalidpublic YearWeek withWeek(int week)
YearWeek
with the week altered.
This returns a year-week with the specified week-of-week-based-year. If the new week is 53 and the year does not have 53 weeks, week one of the following year is selected.
This instance is immutable and unaffected by this method call.
week
- the week-of-week-based-year to set in the returned year-weekYearWeek
based on this year-week with the requested week, not nullDateTimeException
- if the week-of-week-based-year value is invalidpublic YearWeek plusYears(long yearsToAdd)
If the week of this instance is 53 and the new year does not have 53 weeks, the week will be adjusted to be 52.
This instance is immutable and unaffected by this method call.
yearsToAdd
- the years to add, may be negativepublic YearWeek plusWeeks(long weeksToAdd)
This instance is immutable and unaffected by this method call.
weeksToAdd
- the weeks to add, may be negativepublic YearWeek minusYears(long yearsToSubtract)
If the week of this instance is 53 and the new year does not have 53 weeks, the week will be adjusted to be 52.
This instance is immutable and unaffected by this method call.
yearsToSubtract
- the years to subtract, may be negativepublic YearWeek minusWeeks(long weeksToSubtract)
This instance is immutable and unaffected by this method call.
weeksToSubtract
- the weeks to subtract, may be negativepublic <R> R query(TemporalQuery<R> query)
This queries this year-week using the specified query strategy object.
The TemporalQuery
object defines the logic to be used to
obtain the result. Read the documentation of the query to understand
what the result of this method will be.
The result of this method is obtained by invoking the
TemporalQuery.queryFrom(TemporalAccessor)
method on the
specified query passing this
as the argument.
query
in interface TemporalAccessor
R
- the type of the resultquery
- the query to invoke, not nullDateTimeException
- if unable to query (defined by the query)ArithmeticException
- if numeric overflow occurs (defined by the query)public Temporal adjustInto(Temporal temporal)
This returns a temporal object of the same observable type as the input with the week-based-year and week changed to be the same as this.
The adjustment is equivalent to using Temporal.with(TemporalField, long)
twice, passing IsoFields.WEEK_BASED_YEAR
and
IsoFields.WEEK_OF_WEEK_BASED_YEAR
as the fields.
If the specified temporal object does not use the ISO calendar system then
a DateTimeException
is 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 = thisYearWeek.adjustInto(temporal); temporal = temporal.with(thisYearWeek);
This instance is immutable and unaffected by this method call.
adjustInto
in interface TemporalAdjuster
temporal
- the target object to be adjusted, not nullDateTimeException
- if unable to make the adjustmentArithmeticException
- if numeric overflow occurspublic String format(DateTimeFormatter formatter)
This year-week will be passed to the formatter to produce a string.
formatter
- the formatter to use, not nullDateTimeException
- if an error occurs during printingpublic LocalDate atDay(DayOfWeek dayOfWeek)
LocalDate
.
This returns a LocalDate
formed from this year-week and the specified day-of-Week.
This method can be used as part of a chain to produce a date:
LocalDate date = yearWeek.atDay(MONDAY);
dayOfWeek
- the day-of-week to use, not nullpublic int compareTo(YearWeek other)
The comparison is based first on the value of the year, then on the value of the week.
It is "consistent with equals", as defined by Comparable
.
compareTo
in interface Comparable<YearWeek>
other
- the other year-week to compare to, not nullpublic boolean isAfter(YearWeek other)
other
- the other year-week to compare to, not nullpublic boolean isBefore(YearWeek other)
other
- the other year-week to compare to, not nullpublic boolean equals(Object obj)
public int hashCode()
Copyright © 2010–2017 ThreeTen.org. All rights reserved.