Class AgeBetween
java.lang.Object
cloud.opencode.base.date.between.AgeBetween
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 Summary
Modifier and TypeMethodDescriptionstatic intageInYears(LocalDate birthDate) Calculates age in years from birth date to today 从出生日期到今天计算年龄(年)static AgeBetweenCreates an AgeBetween from birth date to a specific reference date 从出生日期到指定参考日期创建AgeBetweenbooleanformat()Formats the age as a string 将年龄格式化为字符串Formats the age as a Chinese string 将年龄格式化为中文字符串static AgeBetweenCreates an AgeBetween from birth date to today 从出生日期到今天创建AgeBetweenstatic AgeBetweenfromBirth(LocalDateTime birthDateTime) Creates an AgeBetween from birth date to today (from LocalDateTime) 从出生日期时间到今天创建AgeBetweenGets the birth date 获取出生日期Gets the Chinese zodiac animal 获取中国生肖Gets the Chinese zodiac animal in Chinese 获取中国生肖(中文)intgetDays()Gets the days component (0-30) 获取天数组件(0-30)longGets the number of days until the next birthday 获取距离下一个生日的天数Gets the last birthday date 获取上一个生日日期intGets the months component (0-11) 获取月份组件(0-11)Gets the next birthday date 获取下一个生日日期Gets the period 获取PeriodGets the reference date 获取参考日期longGets the total age in days 获取总天数的年龄longGets the total age in months 获取总月数的年龄longGets the total age in weeks 获取总周数的年龄intgetYears()Gets the age in complete years 获取完整年数的年龄Gets the Western zodiac sign 获取西方星座Gets the Western zodiac sign in Chinese 获取西方星座(中文)inthashCode()booleanChecks if today is the birthday 检查今天是否是生日toDetail()Gets a detailed age breakdown 获取详细的年龄分解toString()
-
Method Details
-
fromBirth
Creates an AgeBetween from birth date to today 从出生日期到今天创建AgeBetween- Parameters:
birthDate- the birth date | 出生日期- Returns:
- the AgeBetween instance | AgeBetween实例
-
fromBirth
Creates an AgeBetween from birth date to today (from LocalDateTime) 从出生日期时间到今天创建AgeBetween- Parameters:
birthDateTime- the birth date-time | 出生日期时间- Returns:
- the AgeBetween instance | AgeBetween实例
-
at
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
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
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
Gets the last birthday date 获取上一个生日日期- Returns:
- the last birthday | 上一个生日
-
getZodiacSign
-
getZodiacSignChinese
Gets the Western zodiac sign in Chinese 获取西方星座(中文)- Returns:
- the zodiac sign in Chinese | 星座(中文)
-
getChineseZodiac
Gets the Chinese zodiac animal 获取中国生肖- Returns:
- the Chinese zodiac | 生肖
-
getChineseZodiacChinese
Gets the Chinese zodiac animal in Chinese 获取中国生肖(中文)- Returns:
- the Chinese zodiac in Chinese | 生肖(中文)
-
toDetail
-
getBirthDate
-
getReferenceDate
Gets the reference date 获取参考日期- Returns:
- the reference date | 参考日期
-
getPeriod
-
format
-
formatChinese
Formats the age as a Chinese string 将年龄格式化为中文字符串- Returns:
- the formatted age in Chinese | 格式化的年龄(中文)
-
equals
-
hashCode
-
toString
-