Class AgeBetween

java.lang.Object
cloud.opencode.base.date.between.AgeBetween

public final class AgeBetween extends Object
Calculates age from a birth date to a reference date 从出生日期到参考日期计算年龄

This class provides methods to calculate age in various formats, supporting different calendar systems and precision levels.

此类提供多种格式计算年龄的方法,支持不同的日历系统和精度级别。

Features | 主要功能:

  • Age in years, months, days - 年龄(年、月、日)
  • Total age in different units - 不同单位的总年龄
  • Chinese zodiac and Western zodiac support - 生肖和星座支持
  • Birthday checking - 生日检查

Usage Examples | 使用示例:

LocalDate birth = LocalDate.of(1990, 5, 15);

// Calculate age from birth to today
AgeBetween age = AgeBetween.fromBirth(birth);
System.out.println(age.getYears());           // 34
System.out.println(age.isBirthdayToday());    // false

// Calculate age at a specific date
LocalDate refDate = LocalDate.of(2024, 5, 15);
AgeBetween ageAtDate = AgeBetween.at(birth, refDate);
System.out.println(ageAtDate.isBirthdayToday()); // true

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

    • fromBirth

      public static AgeBetween fromBirth(LocalDate birthDate)
      Creates an AgeBetween from birth date to today 从出生日期到今天创建AgeBetween
      Parameters:
      birthDate - the birth date | 出生日期
      Returns:
      the AgeBetween instance | AgeBetween实例
    • fromBirth

      public static AgeBetween fromBirth(LocalDateTime birthDateTime)
      Creates an AgeBetween from birth date to today (from LocalDateTime) 从出生日期时间到今天创建AgeBetween
      Parameters:
      birthDateTime - the birth date-time | 出生日期时间
      Returns:
      the AgeBetween instance | AgeBetween实例
    • at

      public static AgeBetween at(LocalDate birthDate, LocalDate referenceDate)
      Creates an AgeBetween from birth date to a specific reference date 从出生日期到指定参考日期创建AgeBetween
      Parameters:
      birthDate - the birth date | 出生日期
      referenceDate - the reference date | 参考日期
      Returns:
      the AgeBetween instance | AgeBetween实例
    • ageInYears

      public static int ageInYears(LocalDate birthDate)
      Calculates age in years from birth date to today 从出生日期到今天计算年龄(年)
      Parameters:
      birthDate - the birth date | 出生日期
      Returns:
      the age in years | 年龄(年)
    • getYears

      public int getYears()
      Gets the age in complete years 获取完整年数的年龄
      Returns:
      the age in years | 年龄(年)
    • getMonths

      public int getMonths()
      Gets the months component (0-11) 获取月份组件(0-11)
      Returns:
      the months | 月数
    • getDays

      public int getDays()
      Gets the days component (0-30) 获取天数组件(0-30)
      Returns:
      the days | 天数
    • getTotalMonths

      public long getTotalMonths()
      Gets the total age in months 获取总月数的年龄
      Returns:
      the total months | 总月数
    • getTotalDays

      public long getTotalDays()
      Gets the total age in days 获取总天数的年龄
      Returns:
      the total days | 总天数
    • getTotalWeeks

      public long getTotalWeeks()
      Gets the total age in weeks 获取总周数的年龄
      Returns:
      the total weeks | 总周数
    • isBirthdayToday

      public boolean isBirthdayToday()
      Checks if today is the birthday 检查今天是否是生日
      Returns:
      true if today is birthday | 如果今天是生日返回true
    • getNextBirthday

      public LocalDate getNextBirthday()
      Gets the next birthday date 获取下一个生日日期
      Returns:
      the next birthday | 下一个生日
    • getDaysUntilNextBirthday

      public long getDaysUntilNextBirthday()
      Gets the number of days until the next birthday 获取距离下一个生日的天数
      Returns:
      the days until next birthday | 距离下一个生日的天数
    • getLastBirthday

      public LocalDate getLastBirthday()
      Gets the last birthday date 获取上一个生日日期
      Returns:
      the last birthday | 上一个生日
    • getZodiacSign

      public String getZodiacSign()
      Gets the Western zodiac sign 获取西方星座
      Returns:
      the zodiac sign | 星座
    • getZodiacSignChinese

      public String getZodiacSignChinese()
      Gets the Western zodiac sign in Chinese 获取西方星座(中文)
      Returns:
      the zodiac sign in Chinese | 星座(中文)
    • getChineseZodiac

      public String getChineseZodiac()
      Gets the Chinese zodiac animal 获取中国生肖
      Returns:
      the Chinese zodiac | 生肖
    • getChineseZodiacChinese

      public String getChineseZodiacChinese()
      Gets the Chinese zodiac animal in Chinese 获取中国生肖(中文)
      Returns:
      the Chinese zodiac in Chinese | 生肖(中文)
    • toDetail

      public AgeDetail toDetail()
      Gets a detailed age breakdown 获取详细的年龄分解
      Returns:
      the AgeDetail | 年龄详情
    • getBirthDate

      public LocalDate getBirthDate()
      Gets the birth date 获取出生日期
      Returns:
      the birth date | 出生日期
    • getReferenceDate

      public LocalDate getReferenceDate()
      Gets the reference date 获取参考日期
      Returns:
      the reference date | 参考日期
    • getPeriod

      public Period getPeriod()
      Gets the period 获取Period
      Returns:
      the period | Period
    • format

      public String format()
      Formats the age as a string 将年龄格式化为字符串
      Returns:
      the formatted age | 格式化的年龄
    • formatChinese

      public String formatChinese()
      Formats the age as a Chinese string 将年龄格式化为中文字符串
      Returns:
      the formatted age in Chinese | 格式化的年龄(中文)
    • equals

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

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

      public String toString()
      Overrides:
      toString in class Object