File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Precise age calculation.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Precise age calculation.." Watch "Precise age calculation.." New topic
Author

Precise age calculation..

Franck Tranchant
Ranch Hand

Joined: Aug 07, 2003
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 ?


Franck<p>--------------------
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Can you show us the code you have so far?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Franck Tranchant
Ranch Hand

Joined: Aug 07, 2003
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

Joined: Jun 08, 2004
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

Joined: Aug 07, 2003
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

Joined: Jun 08, 2004
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

Joined: Aug 07, 2003
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 ?

Many thanx in advance
Darin Niard
Ranch Hand

Joined: Jun 08, 2004
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

Joined: Jun 09, 2003
Posts: 4632
This needs a lot of testing

Franck Tranchant
Ranch Hand

Joined: Aug 07, 2003
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

Joined: Jun 09, 2003
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

Joined: Dec 10, 2001
Posts: 7023
You might like to take a look at the JavaRanch FAQ on Dates.

http://faq.javaranch.com/view?JavaDatesFaq

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

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Franck Tranchant
Ranch Hand

Joined: Aug 07, 2003
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Precise age calculation..
 
Similar Threads
Youngest Sun Certified Java Programmer
Can anyone send me the code to calculate the years/months/days between the tow dates.
Date?????
reg age cal
Calculation displays scientific format instead of a whole number