Is there an easy way to calculate number of years using standard
Java? I have seen some examples but they use a product using
BigDate. I need something that will calculate number of years taking
into account leap years.
Thanks
Elly
Eric Sosman - 31 Jan 2007 17:11 GMT
esokol@cerner.com wrote On 01/31/07 11:19,:
> Is there an easy way to calculate number of years using standard
> Java? I have seen some examples but they use a product using
> BigDate. I need something that will calculate number of years taking
> into account leap years.
Could you give an example of exactly what you mean by
"calculate number of years?"

Signature
Eric.Sosman@sun.com
Arne Vajhøj - 01 Feb 2007 01:04 GMT
> esokol@cerner.com wrote On 01/31/07 11:19,:
>> Is there an easy way to calculate number of years using standard
[quoted text clipped - 4 lines]
> Could you give an example of exactly what you mean by
> "calculate number of years?"
The subject line gives some hints to what it could mean.
Arne
Arne Vajhøj - 01 Feb 2007 01:03 GMT
> Is there an easy way to calculate number of years using standard
> Java? I have seen some examples but they use a product using
> BigDate. I need something that will calculate number of years taking
> into account leap years.
From some old code:
private static int age(int y, int m, int d) {
Calendar cal = new GregorianCalendar(y, m, d);
Calendar now = new GregorianCalendar();
int res = now.get(Calendar.YEAR) - cal.get(Calendar.YEAR);
if((cal.get(Calendar.MONTH) > now.get(Calendar.MONTH)) ||
(cal.get(Calendar.MONTH) == now.get(Calendar.MONTH) &&
cal.get(Calendar.DAY_OF_MONTH) >
now.get(Calendar.DAY_OF_MONTH))) {
res--;
}
return res;
}
changing from y m d to Date is left as an exercise.
Arne