Class CalendarWeek

All Implemented Interfaces:
Serializable, Comparable<CalendarWeek>, Iterable<PlainDate>, ChronoDisplay, LocalizedPatternSupport, ChronoInterval<PlainDate>

@CalendarType("iso8601") public final class CalendarWeek extends FixedCalendarInterval<CalendarWeek> implements LocalizedPatternSupport

Represents the calendar week starting on Monday according to ISO-8601-paper.

The elements registered by this class are:

This class offers localized formatting, also style-based. However, the week model is not localized and fixed to ISO-8601. Example for using the German abbreviation KW for a calendar week:

     ChronoFormatter<CalendarWeek> f =
          ChronoFormatter.setUp(CalendarWeek.chronology(), Locale.GERMAN)
          .addPattern("w. 'KW'", PatternType.CLDR).build();
     System.out.println(f.format(CalendarWeek.of(2016, 4)); // 4. KW
 

Note: The current calendar week can be determined by an expression like: nowInSystemTime() or in a more general way CalendarWeek current = SystemClock.inLocalView().now(CalendarWeek.chronology()).

Since:
3.21/4.17
Author:
Meno Hochschild
See Also:
Serialized Form
  • Field Details

    • YEAR_OF_WEEKDATE

      @FormattableElement(format="Y") public static final ChronoElement<Integer> YEAR_OF_WEEKDATE

      Defines an element for the week-based year in an ISO-8601-weekdate.

      The week-based year is usually the same as the calendar year. However, at the begin or end of a calendar year the situation is different because the first week of the weekdate can start after New Year and the last week of the weekdate can end before the last day of the calendar year. Examples:

      • Sunday, [1995-01-01] => [1994-W52-7]
      • Tuesday, [1996-31-12] => [1997-W01-2]
    • WEEK_OF_YEAR

      @FormattableElement(format="w") public static final ChronoElement<Integer> WEEK_OF_YEAR

      Element with the week of year in the value range 1-52/53.

      The calendar week always starts on Monday. The first week which has at least four days within current calendar year is considered as the first week of year.

      See Also:
      Weekmodel.ISO, Weekmodel.weekOfYear()
  • Method Details

    • of

      public static CalendarWeek of(int yearOfWeekdate, int weekOfYear)

      Creates a new instance based on given week-based year and week-of-year according to ISO-8601.

      In order to create the last calendar week of given year, it is recommended to use following expression:

           CalendarWeek first = CalendarWeek.of(2016, 1);
           CalendarWeek last = first.withLastWeekOfYear();
       
      Parameters:
      yearOfWeekdate - week-based year within range -999,999,999 / +999,999,999
      weekOfYear - week of year based on ISO-8601 in range 1-52/53
      Returns:
      new instance
      Throws:
      IllegalArgumentException - if any argument is out of range
      See Also:
      isValid(int, int), withLastWeekOfYear()
    • nowInSystemTime

      public static CalendarWeek nowInSystemTime()

      Obtains the current calendar week in system time.

      Convenient short-cut for: SystemClock.inLocalView().now(CalendarWeek.chronology()).

      Returns:
      current calendar week in system time zone using the system clock
      Since:
      3.24/4.20
      See Also:
      SystemClock.inLocalView(), ZonalClock.now(net.time4j.engine.Chronology)
    • at

      public PlainDate at(Weekday dayOfWeek)

      Combines this calendar week with given day of week to a calendar date.

      Parameters:
      dayOfWeek - day of week in range MONDAY - SUNDAY
      Returns:
      calendar date
      Throws:
      IllegalArgumentException - if the result is beyond the maximum of date axis (exotic edge case)
    • getYear

      public int getYear()

      Yields the year number.

      Returns:
      int
      See Also:
      YEAR_OF_WEEKDATE
    • getWeek

      public int getWeek()

      Yields the number of calendar week.

      Note: The last week of year can be obtained by the expression getMaximum(WEEK_OF_YEAR).

      Returns:
      int
      See Also:
      WEEK_OF_YEAR, ChronoEntity.getMaximum(ChronoElement), withLastWeekOfYear()
    • getStart

      public Boundary<PlainDate> getStart()
      Description copied from interface: ChronoInterval

      Yields the lower bound of this interval.

      Specified by:
      getStart in interface ChronoInterval<PlainDate>
      Returns:
      start interval boundary
    • getEnd

      public Boundary<PlainDate> getEnd()
      Description copied from interface: ChronoInterval

      Yields the upper bound of this interval.

      Specified by:
      getEnd in interface ChronoInterval<PlainDate>
      Returns:
      end interval boundary
    • contains

      public boolean contains(PlainDate temporal)
      Description copied from interface: ChronoInterval

      Queries if given time point belongs to this interval.

      Specified by:
      contains in interface ChronoInterval<PlainDate>
      Parameters:
      temporal - time point to be queried
      Returns:
      true if given time point belongs to this interval else false
    • isAfter

      public boolean isAfter(PlainDate temporal)
      Description copied from interface: ChronoInterval

      Is this interval after the given time point?

      Specified by:
      isAfter in interface ChronoInterval<PlainDate>
      Parameters:
      temporal - reference time point
      Returns:
      true if this interval is after given time point else false
    • isBefore

      public boolean isBefore(PlainDate temporal)
      Description copied from interface: ChronoInterval

      Is this interval before the given time point?

      Specified by:
      isBefore in interface ChronoInterval<PlainDate>
      Parameters:
      temporal - reference time point
      Returns:
      true if this interval is before given time point else false
    • isValid

      public static boolean isValid(int yearOfWeekdate, int weekOfYear)

      Validates given year-of-weekdate and week-of-year.

      Parameters:
      yearOfWeekdate - the year of weekdate (can be different from calendar year near New Year)
      weekOfYear - the week of weekbased year
      Returns:
      true if valid else false
      Since:
      4.37
      See Also:
      of(int, int)
    • length

      public int length()

      Calendar week always consist of seven days.

      The only exception is if the length is limited to five days in the last week of year 999,999,999 due to arithmetical reasons.

      Returns:
      7
    • from

      public static CalendarWeek from(GregorianDate date)

      Converts given gregorian date to a calendar week.

      Parameters:
      date - gregorian calendar date (for example PlainDate
      Returns:
      CalendarWeek
      Throws:
      IllegalArgumentException - if given date is invalid
      Since:
      3.28/4.24
    • withLastWeekOfYear

      public CalendarWeek withLastWeekOfYear()

      Yields the last calendar week of the year of this instance.

      Returns:
      calendar week with week number 52 or 53
    • plus

      public CalendarWeek plus(Years<Weekcycle> years)

      Adds given years to this calendar week.

      Parameters:
      years - the count of week-based years to be added
      Returns:
      result of addition
    • plus

      public CalendarWeek plus(Weeks weeks)

      Adds given weeks to this calendar week.

      Parameters:
      weeks - the count of weeks to be added
      Returns:
      result of addition
    • minus

      public CalendarWeek minus(Years<Weekcycle> years)

      Subtracts given years from this calendar week.

      Parameters:
      years - the count of week-based years to be subtracted
      Returns:
      result of subtraction
    • minus

      public CalendarWeek minus(Weeks weeks)

      Subtracts given weeks from this calendar week.

      Parameters:
      weeks - the count of weeks to be subtracted
      Returns:
      result of subtraction
    • compareTo

      public int compareTo(CalendarWeek other)
      Specified by:
      compareTo in interface Comparable<CalendarWeek>
    • iterator

      public Iterator<PlainDate> iterator()

      Iterates over all seven days from Monday to Sunday.

      Specified by:
      iterator in interface Iterable<PlainDate>
      Returns:
      Iterator
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()

      Outputs this instance as a String in CLDR-format "YYYY-'W'ww" (like "2016-W01").

      Overrides:
      toString in class Object
      Returns:
      String
      See Also:
      parseISO(String)
    • parseISO

      public static CalendarWeek parseISO(String text) throws ParseException

      Interpretes given ISO-conforming text as calendar week.

      The underlying parser uses the CLDR-pattern "YYYY[-]'W'ww".

      Parameters:
      text - text to be parsed
      Returns:
      parsed calendar week
      Throws:
      IndexOutOfBoundsException - if given text is empty
      ParseException - if the text is not parseable
      See Also:
      toString()
    • chronology

      public static Chronology<CalendarWeek> chronology()

      Yields the associated chronology.

      Returns:
      the underlying rule engine
    • timeline

      public static TimeLine<CalendarWeek> timeline()

      Obtains a timeline for this type.

      Returns:
      singleton timeline
      Since:
      5.0
    • getFormatPattern

      public String getFormatPattern(FormatStyle style, Locale locale)
      Description copied from interface: LocalizedPatternSupport

      Defines a CLDR-compatible localized format pattern suitable for printing.

      The default implementation delegates to the underlying chronology.

      Specified by:
      getFormatPattern in interface LocalizedPatternSupport
      Parameters:
      style - format style
      locale - language and country setting
      Returns:
      localized format pattern
    • useDynamicFormatPattern

      public boolean useDynamicFormatPattern()
      Description copied from interface: LocalizedPatternSupport

      Determines if any created format pattern uses the state of this instance.

      The default implementation returns false.

      Specified by:
      useDynamicFormatPattern in interface LocalizedPatternSupport
      Returns:
      true if the method getFormatPattern uses the state of this instance else false
      See Also:
      LocalizedPatternSupport.getFormatPattern(FormatStyle, Locale)