Class YearWeek

  • All Implemented Interfaces:
    Serializable, Comparable<YearWeek>, Temporal, TemporalAccessor, TemporalAdjuster

    public final class YearWeek
    extends Object
    implements Temporal, TemporalAdjuster, Comparable<YearWeek>, 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 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​(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​(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​(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:
        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:
        DateTimeException - if either field is invalid
      • from

        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 - the temporal object to convert, not null
        Returns:
        the year-week, not null
        Throws:
        DateTimeException - if unable to convert to a YearWeek
      • parse

        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 - the text to parse such as "2007-W13", not null
        Returns:
        the parsed year-week, not null
        Throws:
        DateTimeParseException - if the text cannot be parsed
      • parse

        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 - the text to parse, not null
        formatter - the formatter to use, not null
        Returns:
        the parsed year-week, not null
        Throws:
        DateTimeParseException - if the text cannot be parsed
      • isSupported

        public boolean isSupported​(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.

        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.

        Specified by:
        isSupported in interface 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​(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.

        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 Temporal
        Parameters:
        unit - the unit to check, null returns false
        Returns:
        true if the unit can be added/subtracted, false if not
      • range

        public ValueRange range​(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 TemporalAccessor
        Parameters:
        field - the field to query the range for, not null
        Returns:
        the range of valid values for the field, not null
        Throws:
        DateTimeException - if the range for the field cannot be obtained
        UnsupportedTemporalTypeException - if the field is not supported
      • get

        public int get​(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 TemporalAccessor
        Parameters:
        field - the field to get, not null
        Returns:
        the value for the field
        Throws:
        DateTimeException - if a value for the field cannot be obtained or the value is outside the range of valid values for the field
        UnsupportedTemporalTypeException - if the field is not supported or the range of values exceeds an int
        ArithmeticException - if numeric overflow occurs
      • getLong

        public long getLong​(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 TemporalAccessor
        Parameters:
        field - the field to get, not null
        Returns:
        the value for the field
        Throws:
        DateTimeException - if a value for the field cannot be obtained
        UnsupportedTemporalTypeException - if the field is not supported
        ArithmeticException - if numeric overflow occurs
      • 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​(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 Temporal
        Parameters:
        adjuster - the adjuster to use, not null
        Returns:
        a YearWeek based on this with the adjustment made, not null
        Throws:
        DateTimeException - if the adjustment cannot be made
        ArithmeticException - if numeric overflow occurs
      • with

        public YearWeek with​(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 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:
        DateTimeException - if the field cannot be set
        UnsupportedTemporalTypeException - if the field is not supported
        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:
        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:
        DateTimeException - if the week-of-week-based-year value is invalid
      • plus

        public YearWeek plus​(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 Temporal
        Parameters:
        amountToAdd - the amount to add, not null
        Returns:
        a YearWeek based on this year-week with the addition made, not null
        Throws:
        DateTimeException - if the addition cannot be made
        ArithmeticException - if numeric overflow occurs
      • plus

        public YearWeek plus​(long amountToAdd,
                             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 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:
        DateTimeException - if the addition cannot be made
        UnsupportedTemporalTypeException - if the unit is not supported
        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​(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 Temporal
        Parameters:
        amountToSubtract - the amount to subtract, not null
        Returns:
        a YearWeek based on this year-week with the subtraction made, not null
        Throws:
        DateTimeException - if the subtraction cannot be made
        ArithmeticException - if numeric overflow occurs
      • minus

        public YearWeek minus​(long amountToSubtract,
                              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 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:
        DateTimeException - if the subtraction cannot be made
        UnsupportedTemporalTypeException - if the unit is not supported
        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​(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 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:
        DateTimeException - if unable to query (defined by the query)
        ArithmeticException - if numeric overflow occurs (defined by the query)
      • adjustInto

        public Temporal adjustInto​(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 TemporalAdjuster
        Parameters:
        temporal - the target object to be adjusted, not null
        Returns:
        the adjusted object, not null
        Throws:
        DateTimeException - if unable to make the adjustment
        ArithmeticException - if numeric overflow occurs
      • until

        public long until​(Temporal endExclusive,
                          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 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:
        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
      • format

        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 - the formatter to use, not null
        Returns:
        the formatted year-week string, not null
        Throws:
        DateTimeException - if an error occurs during printing
      • atDay

        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 - 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 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​(Object obj)
        Checks if this year-week is equal to another year-week.
        Overrides:
        equals in class 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 Object
        Returns:
        a suitable hash code
      • toString

        public 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 Object
        Returns:
        a string representation of this year-week, not null