Date d1 = person1.getDob();
long agediff = d1.getTime() - new Date().getTime();
agediff would give you the difference in number of milliseconds between two dates.
you may put a factor like 18yrs = 568036800000milli seconds.
and compare agediff > 568036800000 or not.
> Date d1 = person1.getDob();
> long agediff = d1.getTime() - new
> Date().getTime();
>
> agediff would give you the difference in number of
> milliseconds between two dates.
>
> you may put a factor like 18yrs = 568036800000milli
> seconds.
>
> and compare agediff > 568036800000 or not.
That doesn't work. Leap years aren't taken into account.
A difference in dates cannot be measured in milliseconds.
if taken account od leap year im considering 365.25 days per year.
therefore
(18) X (365.25) X (24) X (3600) X 1000 = (18) X 31557600000 = 568036800000 milli seconds
therefore if you are calculating it for 14yrs the factor has to be
(14) X (365.25) X (24) X (3600) X 1000 = (14) X 31557600000 = 441806400000 milli seconds
hope that makes sense...
if that did help you don't forget to assign duke stars which you promised.
REGARDS,
RaHuL
> if taken account od leap year im considering 365.25
> days per year.
> therefore
>
> (18) X (365.25) X (24) X (3600) X 1000 = (18) X
> 31557600000 = 568036800000 milli seconds
>
> therefore if you are calculating it for 14yrs the
> factor has to be
>
> (14) X (365.25) X (24) X (3600) X 1000 = (14) X
> 31557600000 = 441806400000 milli seconds
Which is also wrong. Each year does not contain .25 of a leap day.
A year contains either 0 or 1 leap days. Using Calendar is
correct. Trying to use milliseconds is incorrect.
Calculating with hardcoded millis? Amazing ..
Well, generally you should use Calendar to calculate the date difference. Sun Java core API doesn't provide kind of default "elapsedYears" functionality, so that you have to write it yourself. Generally it's just adding the desired difference in a while (olderCal.before(newerCal)) loop and keeping a counter inside.
Example:Calendar birthdate = Calendar.getInstance();
birthdate.clear();
birthdate.set(1977, 7, 18); // Month starts with 0.
Calendar today = Calendar.getInstance();
int elapsedYears = -1;
while (birthdate.before(today)) {
birthdate.add(Calendar.YEAR, 1);
elapsedYears++;
}
System.out.println(elapsedYears);