Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# Precise age calculation..

Franck Tranchant
Ranch Hand
Posts: 75
Hi,

After several attempts to calculate the age of someone in Java, i give up and ask for for help !

I would like to display quite a precise age such as :
"Today, you have 10 years, 3 months and 21 days"

I can easilly get the number of Years and the number of Months but i couldnt succeed in getting the exact number of Days...

Any idea for a proper code to display this result ?

Ilja Preuss
author
Sheriff
Posts: 14112
Can you show us the code you have so far?

Franck Tranchant
Ranch Hand
Posts: 75
Of course.
Here is my current code :

By changing birthday and today values, you'll see that the number of days returned isn't correct...

Any suggestion is welcome.

Darin Niard
Ranch Hand
Posts: 118
It seems to work for me. You need to realize that these date classes are not meant to measure duration. That is, you will never see 1 Year, 0 Days, because that does not exist on a calendar. I'm assuming you are seeing 11 months, 31 days, and you think that is wrong, when in fact is not wrong at all. Think about it. 11 months, 31 days is exactly the same as 1 year.

Franck Tranchant
Ranch Hand
Posts: 75
Okay for this one.
But in some cases, the result sounds really wrong to me.

For example :
birthday = 25/04/2004
today = 25/06/2004

will return :
2 months, 3 days

I would expect 2 months instead....

Darin Niard
Ranch Hand
Posts: 118
It does this because you are storing the difference between dates as a completely different place in time. In your example above:

See, when you subtracted the two, they were 30/31 day months you were dealing with. Now, you are storing it as a completely different year, and since Feb only has 28 days, you now get some extra days. Like I said before, storing a duration in a Date/Calendar isn't what you want to do.
[ July 16, 2004: Message edited by: Darin Niard ]

Franck Tranchant
Ranch Hand
Posts: 75
Ok I see the problem but cannot find a way to solve it..

I thought this kind of issue was trivial, that's why I posted in the 'Beginner' forum, but maybe it's not...

Anyone has a proposition for a proper way to calculate the exact remaining number of days ?

Darin Niard
Ranch Hand
Posts: 118
Well, off the top of my head, you could take the "total days person has been alive" and then expand it out. The only problem is, your highest measurement has to be weeks, because after that, it is no longer constant (months and years vary).

Michael Dunn
Ranch Hand
Posts: 4632
This needs a lot of testing

Franck Tranchant
Ranch Hand
Posts: 75
Thanx Darin and Michael

Michael, I couldn't try your code since I don't have the right version of the JDK, so I did it my way.
It's a little bit longer but I think it works fine.
My purpose was to compare the birthdate of my son with the date of some digital photos and then automaticaly write the right label in an album photo.

Here is my code :

Of course, any comment is welcome !
[ July 22, 2004: Message edited by: Franck Tranchant ]

Michael Dunn
Ranch Hand
Posts: 4632

Michael, I couldn't try your code since I don't have the right version of the JDK

replace the 'while' statement with this (probably a better way to do it, anyway), but still needs testing

while(today.get(dateParts[i]) != birthday.get(dateParts[i]))

Dirk Schreckmann
Sheriff
Posts: 7023
You might like to take a look at the JavaRanch FAQ on Dates.

It includes a section on comparing dates.
[ July 22, 2004: Message edited by: Dirk Schreckmann ]

Franck Tranchant
Ranch Hand
Posts: 75
Michael,

I checked your code and it seem to work fine.
The manual incrementation is quite clever and what a code size reduction ! :-)

Thank you all for the assistance.