Class YearWeek

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Comparable<YearWeek>, java.time.temporal.Temporal, java.time.temporal.TemporalAccessor, java.time.temporal.TemporalAdjuster

    public final class YearWeek
    extends java.lang.Object
    implements java.time.temporal.Temporal, java.time.temporal.TemporalAdjuster, java.lang.Comparable<YearWeek>, java.io.Serializable
    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 ==.

    See Also:
    Serialized Form
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.time.temporal.Temporal adjustInto​(java.time.temporal.Temporal temporal)
      Adjusts the specified temporal object to have this year-week.
      java.time.LocalDate atDay​(java.time.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​(java.lang.Object obj)
      Checks if this year-week is equal to another year-week.
      java.lang.String format​(java.time.format.DateTimeFormatter formatter)
      Formats this year-week using the specified formatter.
      static YearWeek from​(java.time.temporal.TemporalAccessor temporal)
      Obtains an instance of YearWeek from a temporal object.
      int get​(java.time.temporal.TemporalField field)
      Gets the value of the specified field from this year-week as an int.
      long getLong​(java.time.temporal.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​(java.time.temporal.TemporalField field)
      Checks if the specified field is supported.
      boolean isSupported​(java.time.temporal.TemporalUnit unit)
      Checks if the specified unit is supported.
      int lengthOfYear()
      Returns the length of the week-based-year.
      YearWeek minus​(long amountToSubtract, java.time.temporal.TemporalUnit unit)
      Returns a copy of this year-week with the specified amount subtracted.
      YearWeek minus​(java.time.temporal.TemporalAmount amountToSubtract)
      Returns a copy of this year-week with the specified amount subtracted.
      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​(java.time.Clock clock)
      Obtains the current year-week from the specified clock.
      static YearWeek now​(java.time.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 of​(java.time.Year year, int week)
      Obtains an instance of YearWeek from a year and week.
      static YearWeek parse​(java.lang.CharSequence text)
      Obtains an instance of YearWeek from a text string such as 2007-W13.
      static YearWeek parse​(java.lang.CharSequence text, java.time.format.DateTimeFormatter formatter)
      Obtains an instance of YearWeek from a text string using a specific formatter.
      YearWeek plus​(long amountToAdd, java.time.temporal.TemporalUnit unit)
      Returns a copy of this year-week with the specified amount added.
      YearWeek plus​(java.time.temporal.TemporalAmount amountToAdd)
      Returns a copy of this year-week with the specified amount added.
      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​(java.time.temporal.TemporalQuery<R> query)
      Queries this year-week using the specified query.
      java.time.temporal.ValueRange range​(java.time.temporal.TemporalField field)
      Gets the range of valid values for the specified field.
      java.lang.String toString()
      Outputs this year-week as a String, such as 2015-W13.
      long until​(java.time.temporal.Temporal endExclusive, java.time.temporal.TemporalUnit unit)
      Calculates the amount of time until another year-week in terms of the specified unit.
      YearWeek with​(java.time.temporal.TemporalAdjuster adjuster)
      Returns an adjusted copy of this year-week.
      YearWeek with​(java.time.temporal.TemporalField field, long newValue)
      Returns a copy of this year-week with the specified field set to a new value.
      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.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Method Detail

      • now

        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:
        the current year-week using the system clock and default time-zone, not null
      • now

        public static YearWeek now​(java.time.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 - the zone ID to use, not null
        Returns:
        the current year-week using the system clock, not null
      • now

        public static YearWeek now​(java.time.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 - the clock to use, not null
        Returns:
        the current year-week, not null
      • of

        public static YearWeek of​(java.time.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 - the year to represent, not null
        week - the week-of-week-based-year to represent, from 1 to 53
        Returns:
        the year-week, not null
        Throws:
        java.time.DateTimeException - if the week value is invalid
      • of

        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 - the week-based-year to represent, from MIN_YEAR to MAX_YEAR
        week - the week-of-week-based-year to represent, from 1 to 53
        Returns:
        the year-week, not null
        Throws:
        java.time.DateTimeException - if either field is invalid
      • from

        public static YearWeek from​(java.time.temporal.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 - the temporal object to convert, not null
        Returns:
        the year-week, not null
        Throws:
        java.time.DateTimeException - if unable to convert to a YearWeek
      • parse

        public static YearWeek parse​(java.lang.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 - the text to parse such as "2007-W13", not null
        Returns:
        the parsed year-week, not null
        Throws:
        java.time.format.DateTimeParseException - if the text cannot be parsed
      • parse

        public static YearWeek parse​(java.lang.CharSequence text,
                                     java.time.format.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 - the text to parse, not null
        formatter - the formatter to use, not null
        Returns:
        the parsed year-week, not null
        Throws:
        java.time.format.DateTimeParseException - if the text cannot be parsed
      • isSupported

        public boolean isSupported​(java.time.temporal.TemporalField field)
        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.

        If the field is a ChronoField then the query is implemented here. The supported fields are:

        • WEEK_OF_WEEK_BASED_YEAR
        • WEEK_BASED_YEAR
        All 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.

        Specified by:
        isSupported in interface java.time.temporal.TemporalAccessor
        Parameters:
        field - the field to check, null returns false
        Returns:
        true if the field is supported on this year-week, false if not
      • isSupported

        public boolean isSupported​(java.time.temporal.TemporalUnit unit)
        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.

        If the unit is a ChronoUnit then the query is implemented here. 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.

        Specified by:
        isSupported in interface java.time.temporal.Temporal
        Parameters:
        unit - the unit to check, null returns false
        Returns:
        true if the unit can be added/subtracted, false if not
      • range

        public java.time.temporal.ValueRange range​(java.time.temporal.TemporalField field)
        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.

        Specified by:
        range in interface java.time.temporal.TemporalAccessor
        Parameters:
        field - the field to query the range for, not null
        Returns:
        the range of valid values for the field, not null
        Throws:
        java.time.DateTimeException - if the range for the field cannot be obtained
        java.time.temporal.UnsupportedTemporalTypeException - if the field is not supported
      • get

        public int get​(java.time.temporal.TemporalField field)
        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.

        Specified by:
        get in interface java.time.temporal.TemporalAccessor
        Parameters:
        field - the field to get, not null
        Returns:
        the value for the field
        Throws:
        java.time.DateTimeException - if a value for the field cannot be obtained or the value is outside the range of valid values for the field
        java.time.temporal.UnsupportedTemporalTypeException - if the field is not supported or the range of values exceeds an int
        java.lang.ArithmeticException - if numeric overflow occurs
      • getLong

        public long getLong​(java.time.temporal.TemporalField field)
        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.

        Specified by:
        getLong in interface java.time.temporal.TemporalAccessor
        Parameters:
        field - the field to get, not null
        Returns:
        the value for the field
        Throws:
        java.time.DateTimeException - if a value for the field cannot be obtained
        java.time.temporal.UnsupportedTemporalTypeException - if the field is not supported
        java.lang.ArithmeticException - if numeric overflow occurs
      • getYear

        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:
        the week-based-year
      • getWeek

        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:
        the week-of-week-based-year
      • is53WeekYear

        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:
        true if the year has 53 weeks, false otherwise
      • lengthOfYear

        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:
        364 if the year has 52 weeks, 371 if it has 53 weeks
      • with

        public YearWeek with​(java.time.temporal.TemporalAdjuster adjuster)
        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.

        Specified by:
        with in interface java.time.temporal.Temporal
        Parameters:
        adjuster - the adjuster to use, not null
        Returns:
        a YearWeek based on this with the adjustment made, not null
        Throws:
        java.time.DateTimeException - if the adjustment cannot be made
        java.lang.ArithmeticException - if numeric overflow occurs
      • with

        public YearWeek with​(java.time.temporal.TemporalField field,
                             long newValue)
        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.

        Specified by:
        with in interface java.time.temporal.Temporal
        Parameters:
        field - the field to set in the result, not null
        newValue - the new value of the field in the result
        Returns:
        a YearWeek based on this with the specified field set, not null
        Throws:
        java.time.DateTimeException - if the field cannot be set
        java.time.temporal.UnsupportedTemporalTypeException - if the field is not supported
        java.lang.ArithmeticException - if numeric overflow occurs
      • withYear

        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 - the week-based-year to set in the returned year-week
        Returns:
        a YearWeek based on this year-week with the requested year, not null
        Throws:
        java.time.DateTimeException - if the week-based-year value is invalid
      • withWeek

        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 - the week-of-week-based-year to set in the returned year-week
        Returns:
        a YearWeek based on this year-week with the requested week, not null
        Throws:
        java.time.DateTimeException - if the week-of-week-based-year value is invalid
      • plus

        public YearWeek plus​(java.time.temporal.TemporalAmount amountToAdd)
        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.

        Specified by:
        plus in interface java.time.temporal.Temporal
        Parameters:
        amountToAdd - the amount to add, not null
        Returns:
        a YearWeek based on this year-week with the addition made, not null
        Throws:
        java.time.DateTimeException - if the addition cannot be made
        java.lang.ArithmeticException - if numeric overflow occurs
      • plus

        public YearWeek plus​(long amountToAdd,
                             java.time.temporal.TemporalUnit unit)
        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.

        Specified by:
        plus in interface java.time.temporal.Temporal
        Parameters:
        amountToAdd - the amount of the unit to add to the result, may be negative
        unit - the unit of the amount to add, not null
        Returns:
        a YearWeek based on this year-week with the specified amount added, not null
        Throws:
        java.time.DateTimeException - if the addition cannot be made
        java.time.temporal.UnsupportedTemporalTypeException - if the unit is not supported
        java.lang.ArithmeticException - if numeric overflow occurs
      • plusYears

        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 - the years to add, may be negative
        Returns:
        the year-week with the years added, not null
      • plusWeeks

        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 - the weeks to add, may be negative
        Returns:
        the year-week with the weeks added, not null
      • minus

        public YearWeek minus​(java.time.temporal.TemporalAmount amountToSubtract)
        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.

        Specified by:
        minus in interface java.time.temporal.Temporal
        Parameters:
        amountToSubtract - the amount to subtract, not null
        Returns:
        a YearWeek based on this year-week with the subtraction made, not null
        Throws:
        java.time.DateTimeException - if the subtraction cannot be made
        java.lang.ArithmeticException - if numeric overflow occurs
      • minus

        public YearWeek minus​(long amountToSubtract,
                              java.time.temporal.TemporalUnit unit)
        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.

        Specified by:
        minus in interface java.time.temporal.Temporal
        Parameters:
        amountToSubtract - the amount of the unit to subtract from the result, may be negative
        unit - the unit of the amount to subtract, not null
        Returns:
        a YearWeek based on this year-week with the specified amount subtracted, not null
        Throws:
        java.time.DateTimeException - if the subtraction cannot be made
        java.time.temporal.UnsupportedTemporalTypeException - if the unit is not supported
        java.lang.ArithmeticException - if numeric overflow occurs
      • minusYears

        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 - the years to subtract, may be negative
        Returns:
        the year-week with the years subtracted, not null
      • minusWeeks

        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 - the weeks to subtract, may be negative
        Returns:
        the year-week with the weeks subtracted, not null
      • query

        public <R> R query​(java.time.temporal.TemporalQuery<R> 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.

        Specified by:
        query in interface java.time.temporal.TemporalAccessor
        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:
        java.time.DateTimeException - if unable to query (defined by the query)
        java.lang.ArithmeticException - if numeric overflow occurs (defined by the query)
      • adjustInto

        public java.time.temporal.Temporal adjustInto​(java.time.temporal.Temporal temporal)
        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.

        Specified by:
        adjustInto in interface java.time.temporal.TemporalAdjuster
        Parameters:
        temporal - the target object to be adjusted, not null
        Returns:
        the adjusted object, not null
        Throws:
        java.time.DateTimeException - if unable to make the adjustment
        java.lang.ArithmeticException - if numeric overflow occurs
      • until

        public long until​(java.time.temporal.Temporal endExclusive,
                          java.time.temporal.TemporalUnit unit)
        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.

        Specified by:
        until in interface java.time.temporal.Temporal
        Parameters:
        endExclusive - the end date, exclusive, which is converted to a YearWeek, not null
        unit - the unit to measure the amount in, not null
        Returns:
        the amount of time between this year-week and the end year-week
        Throws:
        java.time.DateTimeException - if the amount cannot be calculated, or the end temporal cannot be converted to a YearWeek
        java.time.temporal.UnsupportedTemporalTypeException - if the unit is not supported
        java.lang.ArithmeticException - if numeric overflow occurs
      • format

        public java.lang.String format​(java.time.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 - the formatter to use, not null
        Returns:
        the formatted year-week string, not null
        Throws:
        java.time.DateTimeException - if an error occurs during printing
      • atDay

        public java.time.LocalDate atDay​(java.time.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 - the day-of-week to use, not null
        Returns:
        the date formed from this year-week and the specified day, not null
      • compareTo

        public int compareTo​(YearWeek other)
        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.

        Specified by:
        compareTo in interface java.lang.Comparable<YearWeek>
        Parameters:
        other - the other year-week to compare to, not null
        Returns:
        the comparator value, negative if less, positive if greater
      • isAfter

        public boolean isAfter​(YearWeek other)
        Is this year-week after the specified year-week.
        Parameters:
        other - the other year-week to compare to, not null
        Returns:
        true if this is after the specified year-week
      • isBefore

        public boolean isBefore​(YearWeek other)
        Is this year-week before the specified year-week.
        Parameters:
        other - the other year-week to compare to, not null
        Returns:
        true if this point is before the specified year-week
      • equals

        public boolean equals​(java.lang.Object obj)
        Checks if this year-week is equal to another year-week.
        Overrides:
        equals in class java.lang.Object
        Parameters:
        obj - the object to check, null returns false
        Returns:
        true if this is equal to the other year-week
      • hashCode

        public int hashCode()
        A hash code for this year-week.
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        a suitable hash code
      • toString

        public java.lang.String toString()
        Outputs this year-week as a String, such as 2015-W13.

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

        Overrides:
        toString in class java.lang.Object
        Returns:
        a string representation of this year-week, not null