Class YearQuarter
java.lang.Object
cloud.opencode.base.date.extra.YearQuarter
- All Implemented Interfaces:
Serializable, Comparable<YearQuarter>, Temporal, TemporalAccessor, TemporalAdjuster
public final class YearQuarter
extends Object
implements Temporal, TemporalAdjuster, Comparable<YearQuarter>, Serializable
Year-Quarter combination representing a specific quarter in a specific year
年-季度组合,表示特定年份的特定季度
This class represents a year and quarter combination, such as "2024-Q1" for the first quarter of 2024. It is modeled after ThreeTen-Extra's YearQuarter class.
此类表示年份和季度的组合,例如"2024-Q1"表示2024年第一季度。 设计参考ThreeTen-Extra的YearQuarter类。
Features | 主要功能:
- Create from year and quarter - 从年份和季度创建
- Parse from string format - 从字符串格式解析
- Add/subtract quarters and years - 加减季度和年份
- Get start/end date of quarter - 获取季度的开始/结束日期
- Comparison and equality - 比较和相等性
Usage Examples | 使用示例:
// Create YearQuarter
YearQuarter yq = YearQuarter.of(2024, 1); // 2024-Q1
YearQuarter yq2 = YearQuarter.of(2024, Quarter.Q2); // 2024-Q2
YearQuarter now = YearQuarter.now(); // Current quarter
// Parse from string
YearQuarter parsed = YearQuarter.parse("2024-Q1");
// Calculate
YearQuarter next = yq.plusQuarters(1); // 2024-Q2
YearQuarter prev = yq.minusYears(1); // 2023-Q1
// Get dates
LocalDate start = yq.atStartOfQuarter(); // 2024-01-01
LocalDate end = yq.atEndOfQuarter(); // 2024-03-31
// Compare
boolean before = yq.isBefore(yq2); // 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) atDay(int dayOfQuarter) Gets the date at a specific day within this quarter 获取此季度内特定天的日期Gets the end date of this quarter 获取此季度的结束日期atMonth(int monthOfQuarter) Gets the YearMonth at a specific month within this quarter 获取此季度内特定月份的YearMonthGets the start date of this quarter 获取此季度的开始日期intcompareTo(YearQuarter other) booleanformat()Formats this YearQuarter using the default format (2024-Q1) 使用默认格式(2024-Q1)格式化此YearQuarterformat(DateTimeFormatter formatter) Formats this YearQuarter using the specified formatter 使用指定格式化器格式化此YearQuarterstatic YearQuarterfrom(TemporalAccessor temporal) Creates a YearQuarter from a TemporalAccessor 从TemporalAccessor创建YearQuarterlonggetLong(TemporalField field) Gets the Quarter enum 获取Quarter枚举intGets the quarter value (1-4) 获取季度值(1-4)intgetYear()Gets the year 获取年份inthashCode()booleanisAfter(YearQuarter other) Checks if this YearQuarter is after the specified one 检查此YearQuarter是否在指定的之后booleanisBefore(YearQuarter other) Checks if this YearQuarter is before the specified one 检查此YearQuarter是否在指定的之前booleanChecks if this is the first quarter of the year 检查是否为年度第一季度booleanChecks if this is the last quarter of the year 检查是否为年度最后一个季度booleanChecks if this year is a leap year 检查此年份是否为闰年booleanisSupported(TemporalField field) booleanisSupported(TemporalUnit unit) intGets the length of this quarter in days 获取此季度的天数minusQuarters(long quarters) Subtracts quarters from this YearQuarter 在此YearQuarter基础上减季度数minusYears(long years) Subtracts years from this YearQuarter 在此YearQuarter基础上减年数static YearQuarternow()Gets the current YearQuarter using system default zone 使用系统默认时区获取当前YearQuarterstatic YearQuarterGets the current YearQuarter using the specified clock 使用指定时钟获取当前YearQuarterstatic YearQuarterGets the current YearQuarter for the specified zone 获取指定时区的当前YearQuarterstatic YearQuarterof(int year, int quarter) Creates a YearQuarter from year and quarter value 从年份和季度值创建YearQuarterstatic YearQuarterCreates a YearQuarter from year and Quarter enum 从年份和Quarter枚举创建YearQuarterstatic YearQuarterparse(CharSequence text) Parses a string to YearQuarter 解析字符串为YearQuarterstatic YearQuarterparse(CharSequence text, DateTimeFormatter formatter) Parses a string to YearQuarter using the specified formatter 使用指定格式化器解析字符串为YearQuarterplus(long amountToAdd, TemporalUnit unit) plusQuarters(long quarters) Adds quarters to this YearQuarter 在此YearQuarter基础上加季度数plusYears(long years) Adds years to this YearQuarter 在此YearQuarter基础上加年数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 YearQuarter from year and quarter value 从年份和季度值创建YearQuarter- Parameters:
year- the year | 年份quarter- the quarter value from 1 to 4 | 季度值,范围1到4- Returns:
- the YearQuarter instance | YearQuarter实例
- Throws:
OpenDateException- if the quarter is invalid | 如果季度无效则抛出异常
-
of
Creates a YearQuarter from year and Quarter enum 从年份和Quarter枚举创建YearQuarter- Parameters:
year- the year | 年份quarter- the Quarter enum | Quarter枚举- Returns:
- the YearQuarter instance | YearQuarter实例
- Throws:
NullPointerException- if quarter is null | 如果季度为null则抛出异常
-
from
Creates a YearQuarter from a TemporalAccessor 从TemporalAccessor创建YearQuarter- Parameters:
temporal- the temporal accessor | 时间访问器- Returns:
- the YearQuarter instance | YearQuarter实例
- Throws:
OpenDateException- if unable to convert | 如果无法转换则抛出异常
-
now
Gets the current YearQuarter using system default zone 使用系统默认时区获取当前YearQuarter- Returns:
- the current YearQuarter | 当前YearQuarter
-
now
Gets the current YearQuarter for the specified zone 获取指定时区的当前YearQuarter- Parameters:
zone- the zone ID | 时区ID- Returns:
- the current YearQuarter | 当前YearQuarter
-
now
Gets the current YearQuarter using the specified clock 使用指定时钟获取当前YearQuarter- Parameters:
clock- the clock to use | 要使用的时钟- Returns:
- the current YearQuarter | 当前YearQuarter
-
parse
Parses a string to YearQuarter 解析字符串为YearQuarterSupported formats: "2024-Q1", "2024Q1"
支持的格式:"2024-Q1"、"2024Q1"
- Parameters:
text- the text to parse | 要解析的文本- Returns:
- the parsed YearQuarter | 解析后的YearQuarter
- Throws:
OpenDateException- if the text cannot be parsed | 如果文本无法解析则抛出异常
-
parse
Parses a string to YearQuarter using the specified formatter 使用指定格式化器解析字符串为YearQuarter- Parameters:
text- the text to parse | 要解析的文本formatter- the formatter to use | 要使用的格式化器- Returns:
- the parsed YearQuarter | 解析后的YearQuarter
- Throws:
OpenDateException- if the text cannot be parsed | 如果文本无法解析则抛出异常
-
getYear
public int getYear()Gets the year 获取年份- Returns:
- the year | 年份
-
getQuarterValue
public int getQuarterValue()Gets the quarter value (1-4) 获取季度值(1-4)- Returns:
- the quarter value | 季度值
-
getQuarter
-
isLeapYear
public boolean isLeapYear()Checks if this year is a leap year 检查此年份是否为闰年- Returns:
- true if leap year | 如果是闰年返回true
-
lengthOfQuarter
public int lengthOfQuarter()Gets the length of this quarter in days 获取此季度的天数- Returns:
- the number of days | 天数
-
isFirstQuarter
public boolean isFirstQuarter()Checks if this is the first quarter of the year 检查是否为年度第一季度- Returns:
- true if Q1 | 如果是Q1返回true
-
isLastQuarter
public boolean isLastQuarter()Checks if this is the last quarter of the year 检查是否为年度最后一个季度- Returns:
- true if Q4 | 如果是Q4返回true
-
plusYears
Adds years to this YearQuarter 在此YearQuarter基础上加年数- Parameters:
years- the years to add | 要加的年数- Returns:
- a new YearQuarter | 新的YearQuarter
-
plusQuarters
Adds quarters to this YearQuarter 在此YearQuarter基础上加季度数- Parameters:
quarters- the quarters to add | 要加的季度数- Returns:
- a new YearQuarter | 新的YearQuarter
-
minusYears
Subtracts years from this YearQuarter 在此YearQuarter基础上减年数- Parameters:
years- the years to subtract | 要减的年数- Returns:
- a new YearQuarter | 新的YearQuarter
-
minusQuarters
Subtracts quarters from this YearQuarter 在此YearQuarter基础上减季度数- Parameters:
quarters- the quarters to subtract | 要减的季度数- Returns:
- a new YearQuarter | 新的YearQuarter
-
atStartOfQuarter
Gets the start date of this quarter 获取此季度的开始日期- Returns:
- the first day of the quarter | 季度的第一天
-
atEndOfQuarter
Gets the end date of this quarter 获取此季度的结束日期- Returns:
- the last day of the quarter | 季度的最后一天
-
atDay
Gets the date at a specific day within this quarter 获取此季度内特定天的日期- Parameters:
dayOfQuarter- the day within the quarter (1 to length) | 季度内的天数(1到长度)- Returns:
- the LocalDate | LocalDate
- Throws:
OpenDateException- if the day is invalid | 如果天数无效则抛出异常
-
atMonth
Gets the YearMonth at a specific month within this quarter 获取此季度内特定月份的YearMonth- Parameters:
monthOfQuarter- the month within the quarter (1, 2, or 3) | 季度内的月份(1、2或3)- Returns:
- the YearMonth | YearMonth
- Throws:
OpenDateException- if the month is invalid | 如果月份无效则抛出异常
-
isBefore
Checks if this YearQuarter is before the specified one 检查此YearQuarter是否在指定的之前- Parameters:
other- the other YearQuarter | 另一个YearQuarter- Returns:
- true if before | 如果在之前返回true
-
isAfter
Checks if this YearQuarter is after the specified one 检查此YearQuarter是否在指定的之后- Parameters:
other- the other YearQuarter | 另一个YearQuarter- Returns:
- true if after | 如果在之后返回true
-
compareTo
- Specified by:
compareToin interfaceComparable<YearQuarter>
-
format
Formats this YearQuarter using the default format (2024-Q1) 使用默认格式(2024-Q1)格式化此YearQuarter- Returns:
- the formatted string | 格式化的字符串
-
format
Formats this YearQuarter using the specified formatter 使用指定格式化器格式化此YearQuarter- 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
-