【发布时间】:2023-03-29 23:39:01
【问题描述】:
我有 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;
}
【问题讨论】:
-
但是我的应用程序在 if (noofyears
-
应用崩溃时您选择的日期是哪一天?
-
今天的当前日期
标签: android datepicker