Class CalendarWeek
- All Implemented Interfaces:
Serializable,Comparable<CalendarWeek>,Iterable<PlainDate>,ChronoDisplay,LocalizedPatternSupport,ChronoInterval<PlainDate>
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 Summary
FieldsModifier and TypeFieldDescriptionstatic ChronoElement<Integer>Element with the week of year in the value range1-52/53.static ChronoElement<Integer>Defines an element for the week-based year in an ISO-8601-weekdate. -
Method Summary
Modifier and TypeMethodDescriptionCombines this calendar week with given day of week to a calendar date.static Chronology<CalendarWeek>Yields the associated chronology.intcompareTo(CalendarWeek other)booleanQueries if given time point belongs to this interval.booleanstatic CalendarWeekfrom(GregorianDate date)Converts given gregorian date to a calendar week.getEnd()Yields the upper bound of this interval.getFormatPattern(FormatStyle style, Locale locale)Defines a CLDR-compatible localized format pattern suitable for printing.getStart()Yields the lower bound of this interval.intgetWeek()Yields the number of calendar week.intgetYear()Yields the year number.inthashCode()booleanIs this interval after the given time point?booleanIs this interval before the given time point?static booleanisValid(int yearOfWeekdate, int weekOfYear)Validates given year-of-weekdate and week-of-year.iterator()Iterates over all seven days from Monday to Sunday.intlength()Calendar week always consist of seven days.Subtracts given weeks from this calendar week.Subtracts given years from this calendar week.static CalendarWeekObtains the current calendar week in system time.static CalendarWeekof(int yearOfWeekdate, int weekOfYear)Creates a new instance based on given week-based year and week-of-year according to ISO-8601.static CalendarWeekInterpretes given ISO-conforming text as calendar week.Adds given weeks to this calendar week.Adds given years to this calendar week.static TimeLine<CalendarWeek>timeline()Obtains a timeline for this type.toString()Outputs this instance as a String in CLDR-format "YYYY-'W'ww" (like "2016-W01").booleanDetermines if any created format pattern uses the state of this instance.Yields the last calendar week of the year of this instance.Methods inherited from class net.time4j.range.FixedCalendarInterval
abuts, contains, isBefore, isEmpty, isFinite, isSimultaneous, streamDaily, toFlexInterval, withValueMethods inherited from class net.time4j.engine.ChronoEntity
contains, get, get, getInt, getMaximum, getMinimum, getRegisteredElements, getTimezone, hasTimezone, isValid, isValid, isValid, matches, with, with, with, withMethods inherited from interface net.time4j.range.ChronoInterval
intersects, isAfterMethods inherited from interface java.lang.Iterable
forEach, spliteratorMethods inherited from interface net.time4j.format.LocalizedPatternSupport
getFormatPattern
-
Field Details
-
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
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
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,999weekOfYear- week of year based on ISO-8601 in range1-52/53- Returns:
- new instance
- Throws:
IllegalArgumentException- if any argument is out of range- See Also:
isValid(int, int),withLastWeekOfYear()
-
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
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
Description copied from interface:ChronoIntervalYields the lower bound of this interval.
- Specified by:
getStartin interfaceChronoInterval<PlainDate>- Returns:
- start interval boundary
-
getEnd
Description copied from interface:ChronoIntervalYields the upper bound of this interval.
- Specified by:
getEndin interfaceChronoInterval<PlainDate>- Returns:
- end interval boundary
-
contains
Description copied from interface:ChronoIntervalQueries if given time point belongs to this interval.
- Specified by:
containsin interfaceChronoInterval<PlainDate>- Parameters:
temporal- time point to be queried- Returns:
trueif given time point belongs to this interval elsefalse
-
isAfter
Description copied from interface:ChronoIntervalIs this interval after the given time point?
- Specified by:
isAfterin interfaceChronoInterval<PlainDate>- Parameters:
temporal- reference time point- Returns:
trueif this interval is after given time point elsefalse
-
isBefore
Description copied from interface:ChronoIntervalIs this interval before the given time point?
- Specified by:
isBeforein interfaceChronoInterval<PlainDate>- Parameters:
temporal- reference time point- Returns:
trueif this interval is before given time point elsefalse
-
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:
trueif valid elsefalse- 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,999due to arithmetical reasons.- Returns:
7
-
from
Converts given gregorian date to a calendar week.
- Parameters:
date- gregorian calendar date (for examplePlainDate- Returns:
- CalendarWeek
- Throws:
IllegalArgumentException- if given date is invalid- Since:
- 3.28/4.24
-
withLastWeekOfYear
Yields the last calendar week of the year of this instance.
- Returns:
- calendar week with week number
52or53
-
plus
Adds given years to this calendar week.
- Parameters:
years- the count of week-based years to be added- Returns:
- result of addition
-
plus
Adds given weeks to this calendar week.
- Parameters:
weeks- the count of weeks to be added- Returns:
- result of addition
-
minus
Subtracts given years from this calendar week.
- Parameters:
years- the count of week-based years to be subtracted- Returns:
- result of subtraction
-
minus
Subtracts given weeks from this calendar week.
- Parameters:
weeks- the count of weeks to be subtracted- Returns:
- result of subtraction
-
compareTo
- Specified by:
compareToin interfaceComparable<CalendarWeek>
-
iterator
Iterates over all seven days from Monday to Sunday.
-
equals
-
hashCode
public int hashCode() -
toString
Outputs this instance as a String in CLDR-format "YYYY-'W'ww" (like "2016-W01").
- Overrides:
toStringin classObject- Returns:
- String
- See Also:
parseISO(String)
-
parseISO
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 emptyParseException- if the text is not parseable- See Also:
toString()
-
chronology
Yields the associated chronology.
- Returns:
- the underlying rule engine
-
timeline
Obtains a timeline for this type.
- Returns:
- singleton timeline
- Since:
- 5.0
-
getFormatPattern
Description copied from interface:LocalizedPatternSupportDefines a CLDR-compatible localized format pattern suitable for printing.
The default implementation delegates to the underlying chronology.
- Specified by:
getFormatPatternin interfaceLocalizedPatternSupport- Parameters:
style- format stylelocale- language and country setting- Returns:
- localized format pattern
-
useDynamicFormatPattern
public boolean useDynamicFormatPattern()Description copied from interface:LocalizedPatternSupportDetermines if any created format pattern uses the state of this instance.
The default implementation returns
false.- Specified by:
useDynamicFormatPatternin interfaceLocalizedPatternSupport- Returns:
trueif the methodgetFormatPatternuses the state of this instance elsefalse- See Also:
LocalizedPatternSupport.getFormatPattern(FormatStyle, Locale)
-