gpt4 book ai didi

android - 根据出生日期计算年龄

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:16:24 26 4
gpt4 key购买 nike

<分区>

我有 DatePicker 对话框,当我当时选择日期时我想计算年龄它正在工作但是当我当时选择当年的日期时它显示 -1 年龄而不是 0 那么如何解决这个问题?请帮我解决。我的代码如下:

public int getAge(int year, int month, int day) {

GregorianCalendar cal = new GregorianCalendar();
int y, m, d, noofyears;

y = cal.get(Calendar.YEAR);// current year ,
m = cal.get(Calendar.MONTH);// current month
d = cal.get(Calendar.DAY_OF_MONTH);// current day
cal.set(year, month, day);// here ur date
noofyears = (int) (y - cal.get(Calendar.YEAR));
LOGD("Age......", String.valueOf(noofyears));

if ((m < cal.get(Calendar.MONTH)) || ((m == cal.get(Calendar.MONTH)) && (d < cal.get(Calendar.DAY_OF_MONTH)))) {
--noofyears;
}
LOGD("Age......", String.valueOf(noofyears));
if (noofyears != 0) {
ageCount = noofyears;
} else {
ageCount = 0;
}
if (noofyears < 0)
throw new IllegalArgumentException("age < 0");
return noofyears;
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com