calculate age

E

esokol

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
 
E

Eric Sosman

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?"
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

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
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Eric said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,250
Latest member
Charlesreero

Latest Threads

Top