First example: birthdate :10-01-1991(ddmmyyyy)
CurrentDate :10-01-2017
如果以上是条件,那么我想打印26作为当前的年龄。 第二个例子:出生日期:25-07-1991(ddmmyyyy)
当前日期:10-01-2017 如果上面是条件,那么我想打印25作为当前年龄。如何计算一个人从出生日期的确切年龄在android
请帮我.... !!!!!!
下面是我试过的代码。
private int calculateage(Integer day1, Integer month1, Integer year1)
{
Calendar birthCal = new GregorianCalendar(1991, 01, 10);
Calendar nowCal = new GregorianCalendar();
age = nowCal.get(Calendar.YEAR) - birthCal.get(Calendar.YEAR);
boolean isMonthGreater = birthCal.get(Calendar.MONTH) >= nowCal
.get(Calendar.MONTH);
boolean isMonthSameButDayGreater = birthCal.get(Calendar.MONTH) >= nowCal.get(Calendar.MONTH)
&& birthCal.get(Calendar.DAY_OF_MONTH) >= nowCal
.get(Calendar.DAY_OF_MONTH);
if (age < 18) {
Age = age;
}
else if (isMonthGreater || isMonthSameButDayGreater) {
Age = age - 1;
}
return Age;
}
兄弟我已经尝试过你的方法示例:我已经选择了10-01-1991 frm日历int month1 = now.get(Calendar.MONTH)并且我想在int month1中得到int = 1对吗?但我越来越int = 0,如果我在代码中的任何地方是错误的,那么请让我知道thanx提前。 –