This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DaysOld question

 
Akira Reddy
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 331
2
Java Netbeans IDE Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9475
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for clarifying. I really appreciate your help.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic