aspose file tools*
The moose likes Java in General and the fly likes DaysOld question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "DaysOld question" Watch "DaysOld question" New topic
Author

DaysOld question

Akira Reddy
Greenhorn

Joined: Dec 14, 2012
Posts: 6
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

Joined: Apr 15, 2011
Posts: 318
    
    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)

It's not what your program can do, it's what your users do with the program.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8207
    
  23

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Akira Reddy
Greenhorn

Joined: Dec 14, 2012
Posts: 6
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

Joined: Jan 03, 2004
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

Joined: Dec 14, 2012
Posts: 6
Thank you for clarifying. I really appreciate your help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DaysOld question