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
• Paul Clapham
• Ron McLeod
• Jeanne Boyarsky
• Tim Cooke
Sheriffs:
• Liutauras Vilda
• paul wheaton
• Henry Wong
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Stephan van Hulst
• Carey Brown
• Frits Walraven
Bartenders:
• Piet Souris
• Himai Minh

# DaysOld question

Greenhorn
Posts: 9
• Number of slices to send:
Optional 'thank-you' note:
Hi,
I am trying to solve this below question.I have looked at the JDate and GDate code.
java DaysOld 2000-2-2
You were born on February 2, 2000
Today is April 19, 2013
You are now 4825 days old.

And below is the algorithm that I came up.

so from 2000 to 2013 i find the leap years
2004, 2008, 2012
see if it is leap year or not

for leap years count sum=sum+366 days
else sum=sum+ 365 days

finding days in month from feb 2nd 26+31 feb+mar

and in 4th month i have to add +19 and that gives me 4825.

Is this how I should approach the problem?

Initially I have copied the String argument into .

After this do I have any function which can directly find the difference between dates or
do I have to implement the above algorithm that I have written?

Could you suggest which direction I should be going?

Ranch Hand
Posts: 334
2
• Number of slices to send:
Optional 'thank-you' note:
Well one thing you could do is use the Calendar class.

Get the dates in milliseconds and divide by 86,400,000 (which I think is the number of milliseconds in a day)

Bartender
Posts: 10780
71
• Number of slices to send:
Optional 'thank-you' note:

Joe Areeda wrote:Well one thing you could do is use the Calendar class.
Get the dates in milliseconds and divide by 86,400,000 (which I think is the number of milliseconds in a day)

Actually, those two solution are mutually exclusive. If you go to the trouble of creating a Calendar, there's not much point in converting it back to a timestamp.

@Akira:
First: neither JDate nor GDate are standard Java date/time classes. They appear to be part of the apache XML beans package.

Second: The solution you adopt will depend on whether timezones are significant. If they are, you will pretty much have to use a Calendar; if not, Joe's solution should be just fine.
Either way, you will have to find out how to convert a JDate/GDate to a regular Java date (java.util.Date). I'm not familiar with the classes, so I can't help you there.

Another possibility: Have a look at Joda Time. It has all sorts of goodies for exactly this kind of stuff and, since you're already using a non-standard package, another one won't make that much difference.

HIH

Winston

Akira Reddy
Greenhorn
Posts: 9
• Number of slices to send:
Optional 'thank-you' note:
Hi Joe,
Thank you for replying. I have used the gettimeinmillis from calendar and have implemented it. I got the result. Thanks.

Hi Winston,
I looked at joda time class. And have used daysimplemented method. but why is it that i m getting 4827 instead of 4826. Could you please explain? below is my code

Bartender
Posts: 6109
6
• Number of slices to send:
Optional 'thank-you' note:

Akira Reddy wrote:
I looked at joda time class. And have used daysimplemented method. but why is it that i m getting 4827 instead of 4826. Could you please explain?

Maybe because Java truncates when doing integer division.

Akira Reddy
Greenhorn
Posts: 9
• Number of slices to send:
Optional 'thank-you' note:
Thank you for clarifying. I really appreciate your help.

 What's that smell? Hey, sniff this tiny ad: Free, earth friendly heat - from the CodeRanch trailboss https://www.kickstarter.com/projects/paulwheaton/free-heat