feat: 健康管理系统,代码提交。
This commit is contained in:
@@ -220,7 +220,26 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
||||
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
|
||||
return sdf.format(calendar.getTime());
|
||||
}
|
||||
/**
|
||||
* 获取选取日期的当前月的第一天
|
||||
*/
|
||||
public static String getAgeByBirthday(Date birthday)
|
||||
{
|
||||
// 假设生日是2000年1月1日
|
||||
Calendar birthdayDate = Calendar.getInstance();
|
||||
birthdayDate.setTime(birthday);
|
||||
Calendar currentDate = Calendar.getInstance();
|
||||
|
||||
// 方法三:使用Calendar类计算年龄差距
|
||||
int ageInYears = currentDate.get(Calendar.YEAR) - birthdayDate.get(Calendar.YEAR);
|
||||
|
||||
if (birthdayDate.get(Calendar.MONTH) > currentDate.get(Calendar.MONTH) ||
|
||||
(birthdayDate.get(Calendar.MONTH) == currentDate.get(Calendar.MONTH)
|
||||
&& birthdayDate.get(Calendar.DATE) > currentDate.get(Calendar.DATE))) {
|
||||
ageInYears--;
|
||||
}
|
||||
return ageInYears + "周岁";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user