• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Precise age calculation..

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you show us the code you have so far?
 
Franck Tranchant
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This needs a lot of testing

 
Franck Tranchant
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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]))
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Franck Tranchant
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic