Class YearWeek

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

    public final class YearWeek
    extends java.lang.Object
    implements java.time.temporal.TemporalAccessor, 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.
      int lengthOfYear()
      Returns the length of the week-based-year.
      YearWeek minusWeeks​(long weeksToSubtract)
      Returns a copy of this year-week with the specified number of weeks subtracted.
      YearWeek minusYears​(long yearsToSubtract)
      Returns a copy of this year-week with the specified number of years subtracted.
      static YearWeek now()
      Obtains the current year-week from the system clock in the default time-zone.
      static YearWeek now​(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 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 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.
      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​(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
      • 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
      • 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
      • 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
      • 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
      • 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