Class YearWeek
java.lang.Object
cloud.opencode.base.date.extra.YearWeek
- All Implemented Interfaces:
Serializable, Comparable<YearWeek>, Temporal, TemporalAccessor, TemporalAdjuster
public final class YearWeek
extends Object
implements Temporal, TemporalAdjuster, Comparable<YearWeek>, Serializable
Year-Week combination representing a specific ISO week in a specific year
年-周组合,表示特定年份的特定ISO周
This class represents an ISO week-based year and week combination, such as "2024-W01" for the first week of 2024. The week numbering follows ISO-8601 standard where week 1 is the first week containing at least 4 days of the new year.
此类表示基于ISO的年份和周的组合,例如"2024-W01"表示2024年第一周。 周编号遵循ISO-8601标准,第1周是包含新年至少4天的第一周。
Features | 主要功能:
- Create from week-based year and week - 从周基准年和周创建
- Parse from string format (2024-W01) - 从字符串格式解析
- Add/subtract weeks and years - 加减周和年
- Get specific day of week - 获取特定星期几的日期
- ISO-8601 compliant week numbering - 符合ISO-8601的周编号
Usage Examples | 使用示例:
// Create YearWeek
YearWeek yw = YearWeek.of(2024, 1); // 2024-W01
YearWeek now = YearWeek.now(); // Current week
// Parse from string
YearWeek parsed = YearWeek.parse("2024-W01");
// Calculate
YearWeek next = yw.plusWeeks(1); // 2024-W02
YearWeek prev = yw.minusYears(1); // 2023-W01
// Get specific day
LocalDate monday = yw.atMonday(); // First Monday of week
LocalDate friday = yw.atDay(DayOfWeek.FRIDAY);
// Compare
boolean before = yw.isBefore(next); // true
Performance | 性能特性:
- Immutable and thread-safe - 不可变且线程安全
- Cached formatter for parsing - 解析使用缓存的格式化器
Security | 安全性:
- Thread-safe: Yes - 线程安全: 是
- Immutable: Yes - 不可变: 是
- Since:
- JDK 25, opencode-base-date V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionadjustInto(Temporal temporal) Gets the date for a specific day of week within this week 获取此周内特定星期几的日期atFriday()Gets the Friday of this week 获取此周的周五atMonday()Gets the Monday of this week 获取此周的周一Gets the Saturday of this week 获取此周的周六atSunday()Gets the Sunday of this week 获取此周的周日Gets the Thursday of this week 获取此周的周四Gets the Tuesday of this week 获取此周的周二Gets the Wednesday of this week 获取此周的周三intbooleanformat()Formats this YearWeek using the default format (2024-W01) 使用默认格式(2024-W01)格式化此YearWeekformat(DateTimeFormatter formatter) Formats this YearWeek using the specified formatter 使用指定格式化器格式化此YearWeekstatic YearWeekfrom(TemporalAccessor temporal) Creates a YearWeek from a TemporalAccessor 从TemporalAccessor创建YearWeeklonggetLong(TemporalField field) intgetWeek()Gets the week number (1-52 or 53) 获取周数(1-52或53)intgetYear()Gets the week-based year 获取周基准年inthashCode()booleanChecks if this YearWeek is after the specified one 检查此YearWeek是否在指定的之后booleanChecks if this YearWeek is before the specified one 检查此YearWeek是否在指定的之前booleanChecks if this is the first week of the year 检查是否为年度第一周booleanChecks if this is the last week of the year 检查是否为年度最后一周booleanisSupported(TemporalField field) booleanisSupported(TemporalUnit unit) intGets the number of weeks in this week-based year 获取此周基准年的周数minusWeeks(long weeks) Subtracts weeks from this YearWeek 在此YearWeek基础上减周数minusYears(long years) Subtracts years from this YearWeek 在此YearWeek基础上减年数static YearWeeknow()Gets the current YearWeek using system default zone 使用系统默认时区获取当前YearWeekstatic YearWeekGets the current YearWeek using the specified clock 使用指定时钟获取当前YearWeekstatic YearWeekGets the current YearWeek for the specified zone 获取指定时区的当前YearWeekstatic YearWeekof(int weekBasedYear, int week) Creates a YearWeek from week-based year and week 从周基准年和周创建YearWeekstatic YearWeekparse(CharSequence text) Parses a string to YearWeek 解析字符串为YearWeekstatic YearWeekparse(CharSequence text, DateTimeFormatter formatter) Parses a string to YearWeek using the specified formatter 使用指定格式化器解析字符串为YearWeekplus(long amountToAdd, TemporalUnit unit) plusWeeks(long weeks) Adds weeks to this YearWeek 在此YearWeek基础上加周数plusYears(long years) Adds years to this YearWeek 在此YearWeek基础上加年数range(TemporalField field) toString()longuntil(Temporal endExclusive, TemporalUnit unit) with(TemporalField field, long newValue) Methods inherited from interface TemporalAccessor
get, query
-
Method Details
-
of
Creates a YearWeek from week-based year and week 从周基准年和周创建YearWeek- Parameters:
weekBasedYear- the week-based year | 周基准年week- the week number (1-52 or 53) | 周数(1-52或53)- Returns:
- the YearWeek instance | YearWeek实例
- Throws:
OpenDateException- if the week is invalid for the year | 如果周数对该年无效则抛出异常
-
from
Creates a YearWeek from a TemporalAccessor 从TemporalAccessor创建YearWeek- Parameters:
temporal- the temporal accessor | 时间访问器- Returns:
- the YearWeek instance | YearWeek实例
- Throws:
OpenDateException- if unable to convert | 如果无法转换则抛出异常
-
now
Gets the current YearWeek using system default zone 使用系统默认时区获取当前YearWeek- Returns:
- the current YearWeek | 当前YearWeek
-
now
-
now
-
parse
Parses a string to YearWeek 解析字符串为YearWeekSupported formats: "2024-W01", "2024W01"
支持的格式:"2024-W01"、"2024W01"
- Parameters:
text- the text to parse | 要解析的文本- Returns:
- the parsed YearWeek | 解析后的YearWeek
- Throws:
OpenDateException- if the text cannot be parsed | 如果文本无法解析则抛出异常
-
parse
Parses a string to YearWeek using the specified formatter 使用指定格式化器解析字符串为YearWeek- Parameters:
text- the text to parse | 要解析的文本formatter- the formatter to use | 要使用的格式化器- Returns:
- the parsed YearWeek | 解析后的YearWeek
- Throws:
OpenDateException- if the text cannot be parsed | 如果文本无法解析则抛出异常
-
getYear
public int getYear()Gets the week-based year 获取周基准年- Returns:
- the week-based year | 周基准年
-
getWeek
public int getWeek()Gets the week number (1-52 or 53) 获取周数(1-52或53)- Returns:
- the week number | 周数
-
lengthOfYear
public int lengthOfYear()Gets the number of weeks in this week-based year 获取此周基准年的周数- Returns:
- 52 or 53 | 52或53
-
isFirstWeek
public boolean isFirstWeek()Checks if this is the first week of the year 检查是否为年度第一周- Returns:
- true if week 1 | 如果是第1周返回true
-
isLastWeek
public boolean isLastWeek()Checks if this is the last week of the year 检查是否为年度最后一周- Returns:
- true if last week | 如果是最后一周返回true
-
plusYears
Adds years to this YearWeek 在此YearWeek基础上加年数- Parameters:
years- the years to add | 要加的年数- Returns:
- a new YearWeek | 新的YearWeek
-
plusWeeks
Adds weeks to this YearWeek 在此YearWeek基础上加周数- Parameters:
weeks- the weeks to add | 要加的周数- Returns:
- a new YearWeek | 新的YearWeek
-
minusYears
Subtracts years from this YearWeek 在此YearWeek基础上减年数- Parameters:
years- the years to subtract | 要减的年数- Returns:
- a new YearWeek | 新的YearWeek
-
minusWeeks
Subtracts weeks from this YearWeek 在此YearWeek基础上减周数- Parameters:
weeks- the weeks to subtract | 要减的周数- Returns:
- a new YearWeek | 新的YearWeek
-
atDay
Gets the date for a specific day of week within this week 获取此周内特定星期几的日期- Parameters:
dayOfWeek- the day of week | 星期几- Returns:
- the LocalDate | LocalDate
- Throws:
NullPointerException- if dayOfWeek is null | 如果dayOfWeek为null则抛出异常
-
atMonday
-
atTuesday
-
atWednesday
Gets the Wednesday of this week 获取此周的周三- Returns:
- the Wednesday date | 周三日期
-
atThursday
Gets the Thursday of this week 获取此周的周四- Returns:
- the Thursday date | 周四日期
-
atFriday
-
atSaturday
Gets the Saturday of this week 获取此周的周六- Returns:
- the Saturday date | 周六日期
-
atSunday
-
isBefore
Checks if this YearWeek is before the specified one 检查此YearWeek是否在指定的之前- Parameters:
other- the other YearWeek | 另一个YearWeek- Returns:
- true if before | 如果在之前返回true
-
isAfter
Checks if this YearWeek is after the specified one 检查此YearWeek是否在指定的之后- Parameters:
other- the other YearWeek | 另一个YearWeek- Returns:
- true if after | 如果在之后返回true
-
compareTo
- Specified by:
compareToin interfaceComparable<YearWeek>
-
format
Formats this YearWeek using the default format (2024-W01) 使用默认格式(2024-W01)格式化此YearWeek- Returns:
- the formatted string | 格式化的字符串
-
format
Formats this YearWeek using the specified formatter 使用指定格式化器格式化此YearWeek- Parameters:
formatter- the formatter to use | 要使用的格式化器- Returns:
- the formatted string | 格式化的字符串
-
isSupported
- Specified by:
isSupportedin interfaceTemporalAccessor
-
isSupported
- Specified by:
isSupportedin interfaceTemporal
-
getLong
- Specified by:
getLongin interfaceTemporalAccessor
-
range
- Specified by:
rangein interfaceTemporalAccessor
-
with
-
plus
-
until
-
adjustInto
- Specified by:
adjustIntoin interfaceTemporalAdjuster
-
equals
-
hashCode
-
toString
-