Top Description Fields Constructors Methods
org.threeten.extra

public final Class YearWeek

extends Object
implements Temporal, TemporalAdjuster, Comparable<YearWeek>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.lang.Comparable, java.time.temporal.TemporalAdjuster, java.time.temporal.Temporal, java.time.temporal.TemporalAccessor
Static Imports
java.time.DayOfWeek.MONDAY, .DayOfWeek.THURSDAY, .DayOfWeek.WEDNESDAY, java.time.temporal.ChronoUnit.WEEKS, .IsoFields.WEEK_BASED_YEAR, .IsoFields.WEEK_BASED_YEARS, .IsoFields.WEEK_OF_WEEK_BASED_YEAR

A year-week in the ISO week date system such as 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.

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 ==.

Field Summary

Modifier and TypeField and Description
private static final DateTimeFormatter
PARSER

Parser.

private static final long
serialVersionUID

Serialization version.

private final int
week

The week-of-week-based-year

private final int
year

The week-based-year.

Constructor Summary

AccessConstructor and Description
private
YearWeek(int
the week-based-year to represent, validated from MIN_YEAR to MAX_YEAR
weekBasedYear
,
int
the week to represent, validated
week
)

Constructor.

Method Summary

Modifier and TypeMethod and Description
public Temporal

Returns:

the adjusted object, not null
adjustInto
(Temporal
the target object to be adjusted, not null
temporal
)

Implements java.time.temporal.TemporalAdjuster.adjustInto.

Adjusts the specified temporal object to have this year-week.

public LocalDate

Returns:

the date formed from this year-week and the specified day, not null
atDay
(DayOfWeek
the day-of-week to use, not null
dayOfWeek
)

Combines this year-week with a day-of-week to create a LocalDate.

public int

Returns:

the comparator value, negative if less, positive if greater
compareTo
(YearWeek
the other year-week to compare to, not null
other
)

Implements java.lang.Comparable.compareTo.

Compares this year-week to another

The comparison is based first on the value of the year, then on the value of the week.

private long
public boolean

Returns:

true if this is equal to the other year-week
equals
(Object
the object to check, null returns false
obj
)

Overrides java.lang.Object.equals.

Checks if this year-week is equal to another year-week.

public String

Returns:

the formatted year-week string, not null
format
(DateTimeFormatter
the formatter to use, not null
formatter
)

Formats this year-week using the specified formatter.

public static YearWeek

Returns:

the year-week, not null
from
(TemporalAccessor
the temporal object to convert, not null
temporal
)

Obtains an instance of YearWeek from a temporal object.

public int

Returns:

the value for the field
get
(TemporalField
the field to get, not null
field
)

Overrides default java.time.temporal.TemporalAccessor.get.

Gets the value of the specified field from this year-week as an int.

public long

Returns:

the value for the field
getLong
(TemporalField
the field to get, not null
field
)

Implements java.time.temporal.TemporalAccessor.getLong.

Gets the value of the specified field from this year-week as a long.

public int

Returns:

the week-of-week-based-year
getWeek
()

Gets the week-of-week-based-year field.

public int

Returns:

the week-based-year
getYear
()

Gets the week-based-year field.

public int

Returns:

a suitable hash code
hashCode
()

Overrides java.lang.Object.hashCode.

A hash code for this year-week.

public boolean

Returns:

true if the year has 53 weeks, false otherwise
is53WeekYear
()

Checks if the week-based-year has 53 weeks.

public boolean

Returns:

true if this is after the specified year-week
isAfter
(YearWeek
the other year-week to compare to, not null
other
)

Is this year-week after the specified year-week.

public boolean

Returns:

true if this point is before the specified year-week
isBefore
(YearWeek
the other year-week to compare to, not null
other
)

Is this year-week before the specified year-week.

public boolean

Returns:

true if the field is supported on this year-week, false if not
isSupported
(TemporalField
the field to check, null returns false
field
)

Implements java.time.temporal.TemporalAccessor.isSupported.

Checks if the specified field is supported.

public boolean

Returns:

true if the unit can be added/subtracted, false if not
isSupported
(TemporalUnit
the unit to check, null returns false
unit
)

Implements java.time.temporal.Temporal.isSupported.

Checks if the specified unit is supported.

public int

Returns:

364 if the year has 52 weeks, 371 if it has 53 weeks
lengthOfYear
()

Returns the length of the week-based-year.

public YearWeek

Returns:

a YearWeek based on this year-week with the subtraction made, not null
minus
(TemporalAmount
the amount to subtract, not null
amountToSubtract
)

Overrides default java.time.temporal.Temporal.minus.

Returns a copy of this year-week with the specified amount subtracted.

public YearWeek

Returns:

a YearWeek based on this year-week with the specified amount subtracted, not null
minus
(long
the amount of the unit to subtract from the result, may be negative
amountToSubtract
,
TemporalUnit
the unit of the amount to subtract, not null
unit
)

Overrides default java.time.temporal.Temporal.minus.

Returns a copy of this year-week with the specified amount subtracted.

public YearWeek

Returns:

the year-week with the weeks subtracted, not null
minusWeeks
(long
the weeks to subtract, may be negative
weeksToSubtract
)

Returns a copy of this year-week with the specified number of weeks subtracted.

public YearWeek

Returns:

the year-week with the years subtracted, not null
minusYears
(long
the years to subtract, may be negative
yearsToSubtract
)

Returns a copy of this year-week with the specified number of years subtracted.

public static YearWeek

Returns:

the current year-week using the system clock and default time-zone, not null
now
()

Obtains the current year-week from the system clock in the default time-zone.

public static YearWeek

Returns:

the current year-week using the system clock, not null
now
(ZoneId
the zone ID to use, not null
zone
)

Obtains the current year-week from the system clock in the specified time-zone.

public static YearWeek

Returns:

the current year-week, not null
now
(Clock
the clock to use, not null
clock
)

Obtains the current year-week from the specified clock.

public static YearWeek

Returns:

the year-week, not null
of
(Year
the year to represent, not null
year
,
int
the week-of-week-based-year to represent, from 1 to 53
week
)

Obtains an instance of YearWeek from a year and week.

public static YearWeek

Returns:

the year-week, not null
of
(int
the week-based-year to represent, from MIN_YEAR to MAX_YEAR
weekBasedYear
,
int
the week-of-week-based-year to represent, from 1 to 53
week
)

Obtains an instance of YearWeek from a week-based-year and week.

public static YearWeek

Returns:

the parsed year-week, not null
parse
(CharSequence
the text to parse such as "2007-W13", not null
text
)

Obtains an instance of YearWeek from a text string such as 2007-W13.

public static YearWeek

Returns:

the parsed year-week, not null
parse
(CharSequence
the text to parse, not null
text
,
DateTimeFormatter
the formatter to use, not null
formatter
)

Obtains an instance of YearWeek from a text string using a specific formatter.

public YearWeek

Returns:

a YearWeek based on this year-week with the addition made, not null
plus
(TemporalAmount
the amount to add, not null
amountToAdd
)

Overrides default java.time.temporal.Temporal.plus.

Returns a copy of this year-week with the specified amount added.

public YearWeek

Returns:

a YearWeek based on this year-week with the specified amount added, not null
plus
(long
the amount of the unit to add to the result, may be negative
amountToAdd
,
TemporalUnit
the unit of the amount to add, not null
unit
)

Implements java.time.temporal.Temporal.plus.

Returns a copy of this year-week with the specified amount added.

public YearWeek

Returns:

the year-week with the weeks added, not null
plusWeeks
(long
the weeks to add, may be negative
weeksToAdd
)

Returns a copy of this year-week with the specified number of weeks added.

public YearWeek

Returns:

the year-week with the years added, not null
plusYears
(long
the years to add, may be negative
yearsToAdd
)

Returns a copy of this year-week with the specified number of years added.

public <
the type of the result
R
>
R

Returns:

the query result, null may be returned (defined by the query)
query
(TemporalQuery<R>
the query to invoke, not null
query
)

Overrides default java.time.temporal.TemporalAccessor.query.

Queries this year-week using the specified query.

public ValueRange

Returns:

the range of valid values for the field, not null
range
(TemporalField
the field to query the range for, not null
field
)

Overrides default java.time.temporal.TemporalAccessor.range.

Gets the range of valid values for the specified field.

private Object

Returns:

the valid object, not null
readResolve
()

Validates the input.

public String

Returns:

a string representation of this year-week, not null
toString
()

Overrides java.lang.Object.toString.

Outputs this year-week as a String, such as 2015-W13.

public long

Returns:

the amount of time between this year-week and the end year-week
until
(Temporal
the end date, exclusive, which is converted to a YearWeek, not null
endExclusive
,
TemporalUnit
the unit to measure the amount in, not null
unit
)

Implements java.time.temporal.Temporal.until.

Calculates the amount of time until another year-week in terms of the specified unit.

private static int
weekRange(int weekBasedYear)

private YearWeek

Returns:

the year-week, not null
with
(int
the year to represent, validated from MIN_YEAR to MAX_YEAR
newYear
,
int
the week to represent, validated from 1 to 53
newWeek
)

Returns a copy of this year-week with the new year and week, checking to see if a new object is in fact required.

public YearWeek

Returns:

a YearWeek based on this with the adjustment made, not null
with
(TemporalAdjuster
the adjuster to use, not null
adjuster
)

Overrides default java.time.temporal.Temporal.with.

Returns an adjusted copy of this year-week.

public YearWeek

Returns:

a YearWeek based on this with the specified field set, not null
with
(TemporalField
the field to set in the result, not null
field
,
long
the new value of the field in the result
newValue
)

Implements java.time.temporal.Temporal.with.

Returns a copy of this year-week with the specified field set to a new value.

public YearWeek

Returns:

a YearWeek based on this year-week with the requested week, not null
withWeek
(int
the week-of-week-based-year to set in the returned year-week
week
)

Returns a copy of this YearWeek with the week altered.

public YearWeek

Returns:

a YearWeek based on this year-week with the requested year, not null
withYear
(int
the week-based-year to set in the returned year-week
weekBasedYear
)

Returns a copy of this YearWeek with the week-based-year altered.

private long
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

PARSERback to summary
private static final DateTimeFormatter PARSER

Parser.

serialVersionUIDback to summary
private static final long serialVersionUID

Serialization version.

weekback to summary
private final int week

The week-of-week-based-year

yearback to summary
private final int year

The week-based-year.

Constructor Detail

YearWeekback to summary
private YearWeek(int weekBasedYear, int week)

Constructor.

Parameters
weekBasedYear:int

the week-based-year to represent, validated from MIN_YEAR to MAX_YEAR

week:int

the week to represent, validated

Method Detail

adjustIntoback to summary
public Temporal adjustInto(Temporal temporal)

Implements java.time.temporal.TemporalAdjuster.adjustInto.

Adjusts the specified temporal object to have this year-week.

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.

Parameters
temporal:Temporal

the target object to be adjusted, not null

Returns:Temporal

the adjusted object, not null

Annotations
@Override
Exceptions
DateTimeException:
if unable to make the adjustment
ArithmeticException:
if numeric overflow occurs
atDayback to summary
public LocalDate atDay(DayOfWeek dayOfWeek)

Combines this year-week with a day-of-week to create a 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);
Parameters
dayOfWeek:DayOfWeek

the day-of-week to use, not null

Returns:LocalDate

the date formed from this year-week and the specified day, not null

compareToback to summary
public int compareTo(YearWeek other)

Implements java.lang.Comparable.compareTo.

Compares this year-week to another

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.

Parameters
other:YearWeek

the other year-week to compare to, not null

Returns:int

the comparator value, negative if less, positive if greater

Annotations
@Override
daysUntilback to summary
private long daysUntil(YearWeek end)
equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Checks if this year-week is equal to another year-week.

Parameters
obj:Object

the object to check, null returns false

Returns:boolean

true if this is equal to the other year-week

Annotations
@Override
formatback to summary
public String format(DateTimeFormatter formatter)

Formats this year-week using the specified formatter.

This year-week will be passed to the formatter to produce a string.

Parameters
formatter:DateTimeFormatter

the formatter to use, not null

Returns:String

the formatted year-week string, not null

Exceptions
DateTimeException:
if an error occurs during printing
fromback to summary
public static YearWeek from(TemporalAccessor temporal)

Obtains an instance of 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.

Parameters
temporal:TemporalAccessor

the temporal object to convert, not null

Returns:YearWeek

the year-week, not null

Exceptions
DateTimeException:
if unable to convert to a YearWeek
getback to summary
public int get(TemporalField field)

Overrides default java.time.temporal.TemporalAccessor.get.

Gets the value of the specified field from this year-week as an 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.

Parameters
field:TemporalField

the field to get, not null

Returns:int

the value for the field

Annotations
@Override
Exceptions
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
getLongback to summary
public long getLong(TemporalField field)

Implements java.time.temporal.TemporalAccessor.getLong.

Gets the value of the specified field from this year-week as a 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.

Parameters
field:TemporalField

the field to get, not null

Returns:long

the value for the field

Annotations
@Override
Exceptions
DateTimeException:
if a value for the field cannot be obtained
UnsupportedTemporalTypeException:
if the field is not supported
ArithmeticException:
if numeric overflow occurs
getWeekback to summary
public int getWeek()

Gets the week-of-week-based-year field.

This method returns the primitive int value for the week of the week-based-year.

Returns:int

the week-of-week-based-year

getYearback to summary
public int getYear()

Gets the week-based-year field.

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.

Returns:int

the week-based-year

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

A hash code for this year-week.

Returns:int

a suitable hash code

Annotations
@Override
is53WeekYearback to summary
public boolean is53WeekYear()

Checks if the week-based-year has 53 weeks.

This determines if the year has 53 weeks, returning true. If false, the year has 52 weeks.

Returns:boolean

true if the year has 53 weeks, false otherwise

isAfterback to summary
public boolean isAfter(YearWeek other)

Is this year-week after the specified year-week.

Parameters
other:YearWeek

the other year-week to compare to, not null

Returns:boolean

true if this is after the specified year-week

isBeforeback to summary
public boolean isBefore(YearWeek other)

Is this year-week before the specified year-week.

Parameters
other:YearWeek

the other year-week to compare to, not null

Returns:boolean

true if this point is before the specified year-week

isSupportedback to summary
public boolean isSupported(TemporalField field)

Implements java.time.temporal.TemporalAccessor.isSupported.

Checks if the specified field is supported.

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.

The supported fields are:

  • WEEK_OF_WEEK_BASED_YEAR
  • WEEK_BASED_YEAR
All other 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:TemporalField

the field to check, null returns false

Returns:boolean

true if the field is supported on this year-week, false if not

Annotations
@Override
isSupportedback to summary
public boolean isSupported(TemporalUnit unit)

Implements java.time.temporal.Temporal.isSupported.

Checks if the specified unit is supported.

This checks if the specified unit can be added to, or subtracted from, this date-time. If false, then calling the plus(long, TemporalUnit) and minus methods will throw an exception.

The supported units are:

  • WEEKS
  • WEEK_BASED_YEARS
All other 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:TemporalUnit

the unit to check, null returns false

Returns:boolean

true if the unit can be added/subtracted, false if not

Annotations
@Override
lengthOfYearback to summary
public int lengthOfYear()

Returns the length of the week-based-year.

This returns the length of the year in days, either 364 or 371.

Returns:int

364 if the year has 52 weeks, 371 if it has 53 weeks

minusback to summary
public YearWeek minus(TemporalAmount amountToSubtract)

Overrides default java.time.temporal.Temporal.minus.

Returns a copy of this year-week with the specified amount subtracted.

This returns a YearWeek, based on this one, with the specified amount subtracted. The amount is typically Period but may be any other type implementing the TemporalAmount interface.

The calculation is delegated to the amount object by calling TemporalAmount#subtractFrom(Temporal). The amount implementation is free to implement the subtraction in any way it wishes, however it typically calls back to minus(long, TemporalUnit). Consult the documentation of the amount implementation to determine if it can be successfully subtracted.

This instance is immutable and unaffected by this method call.

Parameters
amountToSubtract:TemporalAmount

the amount to subtract, not null

Returns:YearWeek

a YearWeek based on this year-week with the subtraction made, not null

Annotations
@Override
Exceptions
DateTimeException:
if the subtraction cannot be made
ArithmeticException:
if numeric overflow occurs
minusback to summary
public YearWeek minus(long amountToSubtract, TemporalUnit unit)

Overrides default java.time.temporal.Temporal.minus.

Returns a copy of this year-week with the specified amount subtracted.

This returns a YearWeek, based on this one, with the amount in terms of the unit subtracted. If it is not possible to subtract the amount, because the unit is not supported or for some other reason, an exception is thrown.

This method is equivalent to plus(long, TemporalUnit) with the amount negated. See that method for a full description of how addition, and thus subtraction, works.

This instance is immutable and unaffected by this method call.

Parameters
amountToSubtract:long

the amount of the unit to subtract from the result, may be negative

unit:TemporalUnit

the unit of the amount to subtract, not null

Returns:YearWeek

a YearWeek based on this year-week with the specified amount subtracted, not null

Annotations
@Override
Exceptions
DateTimeException:
if the subtraction cannot be made
UnsupportedTemporalTypeException:
if the unit is not supported
ArithmeticException:
if numeric overflow occurs
minusWeeksback to summary
public YearWeek minusWeeks(long weeksToSubtract)

Returns a copy of this year-week with the specified number of weeks subtracted.

This instance is immutable and unaffected by this method call.

Parameters
weeksToSubtract:long

the weeks to subtract, may be negative

Returns:YearWeek

the year-week with the weeks subtracted, not null

minusYearsback to summary
public YearWeek minusYears(long yearsToSubtract)

Returns a copy of this year-week with the specified number of years subtracted.

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.

Parameters
yearsToSubtract:long

the years to subtract, may be negative

Returns:YearWeek

the year-week with the years subtracted, not null

nowback to summary
public static YearWeek now()

Obtains the current year-week from the system clock in the default time-zone.

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.

Returns:YearWeek

the current year-week using the system clock and default time-zone, not null

nowback to summary
public static YearWeek now(ZoneId zone)

Obtains the current year-week from the system clock in the specified time-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.

Parameters
zone:ZoneId

the zone ID to use, not null

Returns:YearWeek

the current year-week using the system clock, not null

nowback to summary
public static YearWeek now(Clock clock)

Obtains the current year-week from the specified 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.

Parameters
clock:Clock

the clock to use, not null

Returns:YearWeek

the current year-week, not null

ofback to summary
public static YearWeek of(Year year, int week)

Obtains an instance of YearWeek from a year and week.

If the week is 53 and the year does not have 53 weeks, week one of the following year is selected.

Note that this class is based on the week-based-year which aligns to Monday to Sunday weeks, whereas Year is intended to represent standard years aligned from January to December. This difference may be seen at the start and/or end of the year. This method treats the standard year as though it is the week-based-year. Thus, YearWeek.of(Year.of(2020), 1) creates an object where Monday and Tuesday of the week are actually the last two days of 2019.

Parameters
year:Year

the year to represent, not null

week:int

the week-of-week-based-year to represent, from 1 to 53

Returns:YearWeek

the year-week, not null

Exceptions
DateTimeException:
if the week value is invalid
ofback to summary
public static YearWeek of(int weekBasedYear, int week)

Obtains an instance of 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.

Parameters
weekBasedYear:int

the week-based-year to represent, from MIN_YEAR to MAX_YEAR

week:int

the week-of-week-based-year to represent, from 1 to 53

Returns:YearWeek

the year-week, not null

Exceptions
DateTimeException:
if either field is invalid
parseback to summary
public static YearWeek parse(CharSequence text)

Obtains an instance of 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.

Parameters
text:CharSequence

the text to parse such as "2007-W13", not null

Returns:YearWeek

the parsed year-week, not null

Annotations
@FromString
Exceptions
DateTimeParseException:
if the text cannot be parsed
parseback to summary
public static YearWeek parse(CharSequence text, DateTimeFormatter formatter)

Obtains an instance of YearWeek from a text string using a specific formatter.

The text is parsed using the formatter, returning a year-week.

Parameters
text:CharSequence

the text to parse, not null

formatter:DateTimeFormatter

the formatter to use, not null

Returns:YearWeek

the parsed year-week, not null

Exceptions
DateTimeParseException:
if the text cannot be parsed
plusback to summary
public YearWeek plus(TemporalAmount amountToAdd)

Overrides default java.time.temporal.Temporal.plus.

Returns a copy of this year-week with the specified amount added.

This returns a YearWeek, based on this one, with the specified amount added. The amount is typically Period but may be any other type implementing the TemporalAmount interface.

The calculation is delegated to the amount object by calling TemporalAmount#addTo(Temporal). The amount implementation is free to implement the addition in any way it wishes, however it typically calls back to plus(long, TemporalUnit). Consult the documentation of the amount implementation to determine if it can be successfully added.

This instance is immutable and unaffected by this method call.

Parameters
amountToAdd:TemporalAmount

the amount to add, not null

Returns:YearWeek

a YearWeek based on this year-week with the addition made, not null

Annotations
@Override
Exceptions
DateTimeException:
if the addition cannot be made
ArithmeticException:
if numeric overflow occurs
plusback to summary
public YearWeek plus(long amountToAdd, TemporalUnit unit)

Implements java.time.temporal.Temporal.plus.

Returns a copy of this year-week with the specified amount added.

This returns a YearWeek, based on this one, with the amount in terms of the unit added. If it is not possible to add the amount, because the unit is not supported or for some other reason, an exception is thrown.

If the field is a ChronoUnit then the addition is implemented here. The supported fields behave as follows:

  • WEEKS - Returns a YearWeek with the weeks added as per plusWeeks(long).
  • WEEK_BASED_YEARS - Returns a YearWeek with the years added as per plusYears(long).

All ChronoUnit instances will throw an UnsupportedTemporalTypeException.

If the field is not a ChronoUnit, then the result of this method is obtained by invoking TemporalUnit.addTo(Temporal, long) passing this as the argument. In this case, the unit determines whether and how to perform the addition.

This instance is immutable and unaffected by this method call.

Parameters
amountToAdd:long

the amount of the unit to add to the result, may be negative

unit:TemporalUnit

the unit of the amount to add, not null

Returns:YearWeek

a YearWeek based on this year-week with the specified amount added, not null

Annotations
@Override
Exceptions
DateTimeException:
if the addition cannot be made
UnsupportedTemporalTypeException:
if the unit is not supported
ArithmeticException:
if numeric overflow occurs
plusWeeksback to summary
public YearWeek plusWeeks(long weeksToAdd)

Returns a copy of this year-week with the specified number of weeks added.

This instance is immutable and unaffected by this method call.

Parameters
weeksToAdd:long

the weeks to add, may be negative

Returns:YearWeek

the year-week with the weeks added, not null

plusYearsback to summary
public YearWeek plusYears(long yearsToAdd)

Returns a copy of this year-week with the specified number of years added.

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.

Parameters
yearsToAdd:long

the years to add, may be negative

Returns:YearWeek

the year-week with the years added, not null

queryback to summary
public <R> R query(TemporalQuery<R> query)

Overrides default java.time.temporal.TemporalAccessor.query.

Queries this year-week using the specified 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.

Parameters
<R>
the type of the result
query:TemporalQuery<R>

the query to invoke, not null

Returns:R

the query result, null may be returned (defined by the query)

Annotations
@SuppressWarnings:unchecked
@Override
Exceptions
DateTimeException:
if unable to query (defined by the query)
ArithmeticException:
if numeric overflow occurs (defined by the query)
rangeback to summary
public ValueRange range(TemporalField field)

Overrides default java.time.temporal.TemporalAccessor.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 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.

Parameters
field:TemporalField

the field to query the range for, not null

Returns:ValueRange

the range of valid values for the field, not null

Annotations
@Override
Exceptions
DateTimeException:
if the range for the field cannot be obtained
UnsupportedTemporalTypeException:
if the field is not supported
readResolveback to summary
private Object readResolve()

Validates the input.

Returns:Object

the valid object, not null

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Outputs this year-week as a String, such as 2015-W13.

The output will be in the format YYYY-'W'ww:

Returns:String

a string representation of this year-week, not null

Annotations
@Override
@ToString
untilback to summary
public long until(Temporal endExclusive, TemporalUnit unit)

Implements java.time.temporal.Temporal.until.

Calculates the amount of time until another year-week in terms of the specified unit.

This calculates the amount of time between two YearWeek objects in terms of a single TemporalUnit. The start and end points are this and the specified year-week. The result will be negative if the end is before the start. The Temporal passed to this method is converted to a YearWeek using from(TemporalAccessor). For example, the period in years between two year-weeks can be calculated using startYearWeek.until(endYearWeek, YEARS).

The calculation returns a whole number, representing the number of complete units between the two year-weeks. For example, the period in years between 2012-W23 and 2032-W22 will only be 9 years as it is one week short of 10 years.

There are two equivalent ways of using this method. The first is to invoke this method. The second is to use TemporalUnit#between(Temporal, Temporal):

  // these two lines are equivalent
  amount = start.until(end, WEEKS);
  amount = WEEKS.between(start, end);
The choice should be made based on which makes the code more readable.

The calculation is implemented in this method for units WEEKS and WEEK_BASED_YEARS. Other ChronoUnit values will throw an exception.

If the unit is not a ChronoUnit, then the result of this method is obtained by invoking TemporalUnit.between(Temporal, Temporal) passing this as the first argument and the converted input temporal as the second argument.

This instance is immutable and unaffected by this method call.

Parameters
endExclusive:Temporal

the end date, exclusive, which is converted to a YearWeek, not null

unit:TemporalUnit

the unit to measure the amount in, not null

Returns:long

the amount of time between this year-week and the end year-week

Annotations
@Override
Exceptions
DateTimeException:
if the amount cannot be calculated, or the end temporal cannot be converted to a YearWeek
UnsupportedTemporalTypeException:
if the unit is not supported
ArithmeticException:
if numeric overflow occurs
weekRangeback to summary
private static int weekRange(int weekBasedYear)
withback to summary
private YearWeek with(int newYear, int newWeek)

Returns a copy of this year-week with the new year and week, checking to see if a new object is in fact required.

Parameters
newYear:int

the year to represent, validated from MIN_YEAR to MAX_YEAR

newWeek:int

the week to represent, validated from 1 to 53

Returns:YearWeek

the year-week, not null

withback to summary
public YearWeek with(TemporalAdjuster adjuster)

Overrides default java.time.temporal.Temporal.with.

Returns an adjusted copy of this year-week.

This returns a YearWeek, based on this one, with the year-week adjusted. The adjustment takes place using the specified adjuster strategy object. Read the documentation of the adjuster to understand what adjustment will be made.

The result of this method is obtained by invoking the TemporalAdjuster#adjustInto(Temporal) method on the specified adjuster passing this as the argument.

This instance is immutable and unaffected by this method call.

Parameters
adjuster:TemporalAdjuster

the adjuster to use, not null

Returns:YearWeek

a YearWeek based on this with the adjustment made, not null

Annotations
@Override
Exceptions
DateTimeException:
if the adjustment cannot be made
ArithmeticException:
if numeric overflow occurs
withback to summary
public YearWeek with(TemporalField field, long newValue)

Implements java.time.temporal.Temporal.with.

Returns a copy of this year-week with the specified field set to a new value.

This returns a YearWeek, based on this one, with the value for the specified field changed. This can be used to change any supported field, such as the year or week. If it is not possible to set 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 adjustment is implemented here. The supported fields behave as follows:

  • WEEK_OF_WEEK_BASED_YEAR - Returns a YearWeek with the specified week-of-year set as per withWeek(int).
  • WEEK_BASED_YEAR - Returns a YearWeek with the specified year set as per withYear(int).

All ChronoField instances will throw an UnsupportedTemporalTypeException.

If the field is not a ChronoField, then the result of this method is obtained by invoking TemporalField.adjustInto(Temporal, long) passing this as the argument. In this case, the field determines whether and how to adjust the instant.

This instance is immutable and unaffected by this method call.

Parameters
field:TemporalField

the field to set in the result, not null

newValue:long

the new value of the field in the result

Returns:YearWeek

a YearWeek based on this with the specified field set, not null

Annotations
@Override
Exceptions
DateTimeException:
if the field cannot be set
UnsupportedTemporalTypeException:
if the field is not supported
ArithmeticException:
if numeric overflow occurs
withWeekback to summary
public YearWeek withWeek(int week)

Returns a copy of this 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.

Parameters
week:int

the week-of-week-based-year to set in the returned year-week

Returns:YearWeek

a YearWeek based on this year-week with the requested week, not null

Exceptions
DateTimeException:
if the week-of-week-based-year value is invalid
withYearback to summary
public YearWeek withYear(int weekBasedYear)

Returns a copy of this 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.

Parameters
weekBasedYear:int

the week-based-year to set in the returned year-week

Returns:YearWeek

a YearWeek based on this year-week with the requested year, not null

Exceptions
DateTimeException:
if the week-based-year value is invalid
yearsUntilback to summary
private long yearsUntil(YearWeek end)