This week's book giveaway is in the Kotlin forum.We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# DaysOld question

Akira Reddy
Greenhorn
Posts: 9
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?

Joe Areeda
Ranch Hand
Posts: 334
2
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)

Winston Gutkowski
Bartender
Posts: 10573
65
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
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

Jeff Verdegan
Bartender
Posts: 6109
6
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
Thank you for clarifying. I really appreciate your help.

 It is sorta covered in the JavaRanch Style Guide.