Class YearHalf
java.lang.Object
cloud.opencode.base.date.extra.YearHalf
- All Implemented Interfaces:
Serializable, Comparable<YearHalf>, Temporal, TemporalAccessor, TemporalAdjuster
public final class YearHalf
extends Object
implements Temporal, TemporalAdjuster, Comparable<YearHalf>, Serializable
A year-half representation (e.g., 2024-H1, 2024-H2)
年-半年表示(如2024-H1、2024-H2)
This class represents a half of a year, useful for bi-annual reporting and financial period calculations.
此类表示一年中的半年,适用于半年度报告和财务周期计算。
Features | 主要功能:
- Year-half representation - 年-半年表示
- Parsing and formatting - 解析和格式化
- Date range operations - 日期范围操作
- Half-year arithmetic - 半年算术
Usage Examples | 使用示例:
YearHalf yh = YearHalf.of(2024, Half.H1);
YearHalf next = yh.plusHalves(1); // 2024-H2
LocalDate start = yh.atStart(); // 2024-01-01
LocalDate end = yh.atEnd(); // 2024-06-30
Security | 安全性:
- Thread-safe: Yes (immutable) - 线程安全: 是(不可变)
- Null-safe: Yes (with explicit null checks) - 空值安全: 是(有明确的空值检查)
- Since:
- JDK 25, opencode-base-date V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumEnum representing the two halves of a year 表示一年两半的枚举 -
Method Summary
Modifier and TypeMethodDescriptionadjustInto(Temporal temporal) atDay(int month, int dayOfMonth) Gets a specific date in this year-half 获取此年-半年中的特定日期atEnd()Gets the end date of this year-half 获取此年-半年的结束日期atStart()Gets the start date of this year-half 获取此年-半年的开始日期intbooleanstatic YearHalffrom(TemporalAccessor temporal) Creates a YearHalf from a TemporalAccessor 从TemporalAccessor创建YearHalfintget(TemporalField field) getHalf()Gets the half 获取半年intGets the half value (1 or 2) 获取半年值(1或2)longgetLong(TemporalField field) intgetYear()Gets the year 获取年份inthashCode()booleanisSupported(TemporalField field) booleanisSupported(TemporalUnit unit) intGets the length of this year-half in days 获取此年-半年的天数intGets the length of this year-half in months 获取此年-半年的月数minusHalves(long halves) Subtracts halves from this year-half 从此年-半年减去半年minusYears(long years) Subtracts years from this year-half 从此年-半年减去年static YearHalfnow()Gets the current YearHalf 获取当前YearHalfstatic YearHalfof(int year, int half) Creates a YearHalf from year and half value 从年份和半年值创建YearHalfstatic YearHalfof(int year, YearHalf.Half half) Creates a YearHalf from year and half 从年份和半年创建YearHalfstatic YearHalfparse(CharSequence text) Parses a YearHalf from a string 从字符串解析YearHalfplus(long amountToAdd, TemporalUnit unit) plusHalves(long halves) Adds halves to this year-half 向此年-半年添加半年plusYears(long years) Adds years to this year-half 向此年-半年添加年<R> Rquery(TemporalQuery<R> query) range(TemporalField field) toString()longuntil(Temporal endExclusive, TemporalUnit unit) with(TemporalField field, long newValue)
-
Method Details
-
of
Creates a YearHalf from year and half 从年份和半年创建YearHalf- Parameters:
year- the year | 年份half- the half | 半年- Returns:
- the YearHalf | YearHalf
-
of
Creates a YearHalf from year and half value 从年份和半年值创建YearHalf- Parameters:
year- the year | 年份half- the half value (1 or 2) | 半年值(1或2)- Returns:
- the YearHalf | YearHalf
-
now
Gets the current YearHalf 获取当前YearHalf- Returns:
- the current YearHalf | 当前YearHalf
-
from
Creates a YearHalf from a TemporalAccessor 从TemporalAccessor创建YearHalf- Parameters:
temporal- the temporal | 时间- Returns:
- the YearHalf | YearHalf
-
parse
Parses a YearHalf from a string 从字符串解析YearHalf- Parameters:
text- the text to parse (e.g., "2024-H1") | 要解析的文本- Returns:
- the YearHalf | YearHalf
-
getYear
public int getYear()Gets the year 获取年份- Returns:
- the year | 年份
-
getHalf
-
getHalfValue
public int getHalfValue()Gets the half value (1 or 2) 获取半年值(1或2)- Returns:
- the half value | 半年值
-
atStart
Gets the start date of this year-half 获取此年-半年的开始日期- Returns:
- the start date | 开始日期
-
atEnd
Gets the end date of this year-half 获取此年-半年的结束日期- Returns:
- the end date | 结束日期
-
atDay
Gets a specific date in this year-half 获取此年-半年中的特定日期- Parameters:
month- the month (1-6 for H1, 7-12 for H2) | 月份dayOfMonth- the day of month | 日期- Returns:
- the date | 日期
-
plusHalves
Adds halves to this year-half 向此年-半年添加半年- Parameters:
halves- the halves to add | 要添加的半年数- Returns:
- the resulting year-half | 结果年-半年
-
minusHalves
Subtracts halves from this year-half 从此年-半年减去半年- Parameters:
halves- the halves to subtract | 要减去的半年数- Returns:
- the resulting year-half | 结果年-半年
-
plusYears
Adds years to this year-half 向此年-半年添加年- Parameters:
years- the years to add | 要添加的年数- Returns:
- the resulting year-half | 结果年-半年
-
minusYears
Subtracts years from this year-half 从此年-半年减去年- Parameters:
years- the years to subtract | 要减去的年数- Returns:
- the resulting year-half | 结果年-半年
-
lengthInDays
public int lengthInDays()Gets the length of this year-half in days 获取此年-半年的天数- Returns:
- the length in days | 天数
-
lengthInMonths
public int lengthInMonths()Gets the length of this year-half in months 获取此年-半年的月数- Returns:
- the length in months (always 6) | 月数(始终为6)
-
isSupported
- Specified by:
isSupportedin interfaceTemporalAccessor
-
getLong
- Specified by:
getLongin interfaceTemporalAccessor
-
range
- Specified by:
rangein interfaceTemporalAccessor
-
get
- Specified by:
getin interfaceTemporalAccessor
-
query
- Specified by:
queryin interfaceTemporalAccessor
-
with
-
plus
-
until
-
isSupported
- Specified by:
isSupportedin interfaceTemporal
-
adjustInto
- Specified by:
adjustIntoin interfaceTemporalAdjuster
-
compareTo
- Specified by:
compareToin interfaceComparable<YearHalf>
-
equals
-
hashCode
-
toString
-