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:
  • Method Details

    • of

      public static YearHalf of(int year, YearHalf.Half half)
      Creates a YearHalf from year and half 从年份和半年创建YearHalf
      Parameters:
      year - the year | 年份
      half - the half | 半年
      Returns:
      the YearHalf | YearHalf
    • of

      public static YearHalf of(int year, int half)
      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

      public static YearHalf now()
      Gets the current YearHalf 获取当前YearHalf
      Returns:
      the current YearHalf | 当前YearHalf
    • from

      public static YearHalf from(TemporalAccessor temporal)
      Creates a YearHalf from a TemporalAccessor 从TemporalAccessor创建YearHalf
      Parameters:
      temporal - the temporal | 时间
      Returns:
      the YearHalf | YearHalf
    • parse

      public static YearHalf parse(CharSequence text)
      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

      public YearHalf.Half getHalf()
      Gets the half 获取半年
      Returns:
      the half | 半年
    • getHalfValue

      public int getHalfValue()
      Gets the half value (1 or 2) 获取半年值(1或2)
      Returns:
      the half value | 半年值
    • atStart

      public LocalDate atStart()
      Gets the start date of this year-half 获取此年-半年的开始日期
      Returns:
      the start date | 开始日期
    • atEnd

      public LocalDate atEnd()
      Gets the end date of this year-half 获取此年-半年的结束日期
      Returns:
      the end date | 结束日期
    • atDay

      public LocalDate atDay(int month, int dayOfMonth)
      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

      public YearHalf plusHalves(long halves)
      Adds halves to this year-half 向此年-半年添加半年
      Parameters:
      halves - the halves to add | 要添加的半年数
      Returns:
      the resulting year-half | 结果年-半年
    • minusHalves

      public YearHalf minusHalves(long halves)
      Subtracts halves from this year-half 从此年-半年减去半年
      Parameters:
      halves - the halves to subtract | 要减去的半年数
      Returns:
      the resulting year-half | 结果年-半年
    • plusYears

      public YearHalf plusYears(long years)
      Adds years to this year-half 向此年-半年添加年
      Parameters:
      years - the years to add | 要添加的年数
      Returns:
      the resulting year-half | 结果年-半年
    • minusYears

      public YearHalf minusYears(long years)
      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

      public boolean isSupported(TemporalField field)
      Specified by:
      isSupported in interface TemporalAccessor
    • getLong

      public long getLong(TemporalField field)
      Specified by:
      getLong in interface TemporalAccessor
    • range

      public ValueRange range(TemporalField field)
      Specified by:
      range in interface TemporalAccessor
    • get

      public int get(TemporalField field)
      Specified by:
      get in interface TemporalAccessor
    • query

      public <R> R query(TemporalQuery<R> query)
      Specified by:
      query in interface TemporalAccessor
    • with

      public Temporal with(TemporalField field, long newValue)
      Specified by:
      with in interface Temporal
    • plus

      public Temporal plus(long amountToAdd, TemporalUnit unit)
      Specified by:
      plus in interface Temporal
    • until

      public long until(Temporal endExclusive, TemporalUnit unit)
      Specified by:
      until in interface Temporal
    • isSupported

      public boolean isSupported(TemporalUnit unit)
      Specified by:
      isSupported in interface Temporal
    • adjustInto

      public Temporal adjustInto(Temporal temporal)
      Specified by:
      adjustInto in interface TemporalAdjuster
    • compareTo

      public int compareTo(YearHalf other)
      Specified by:
      compareTo in interface Comparable<YearHalf>
    • equals

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

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

      public String toString()
      Overrides:
      toString in class Object