Shoutbox

Formula to calculate days lived? - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: Formula to calculate days lived? (/showthread.php?tid=68301)

Formula to calculate days lived? by albert on 11-11-2006 at 09:14 PM

I'm coding this program in Java and I absolutely need a formula to be able to calculte the numbers of days lived.

I searched the net, I couldn't find much.

What I came up with is this :

[ {365 * (currentyear -yearborn)} + {(currentyear -yearborn) / 4} ] - [(daysofyearthatpassed*) + (dayborn)] + [(daysofyearthatpassed*)+(currentday)]


daysofyearthatpassed* : depending on which month and if the year has 366 or 365 days, I calculated an amount that I just add to the day.



As you can see this isn't working.. So if you guys find a better way or can actually correct this, please don't be shy to do so.


RE: Formula to calculate days lived? by foaly on 11-11-2006 at 10:24 PM

couldn't you just use something like:
Date today = new Date();
Date birth =new Date(.....);

and subtract them? although the date object sucks in java...
and semi-replaced by Calender... maybe the api is more helpfull...


RE: Formula to calculate days lived? by qgroessl on 11-11-2006 at 10:25 PM

No... cause you'd need to add in leap years and such.


RE: Formula to calculate days lived? by CookieRevised on 11-12-2006 at 12:37 AM

quote:
Originally posted by alby
I searched the net, I couldn't find much.
I searched the net and found tons :p

search with "Java Datediff" ;)

http://forum.java.sun.com/thread.jspa?threadID=48...&messageID=2292012
http://forum.java.sun.com/thread.jspa?forumID=31&threadID=395202
http://www.java2s.com/Code/Java/Development-Class...etweentwodates.htm
http://jalbum.net/api/se/datadosen/util/Dates.html
http://www.rupertjones.com/dateutil/docs/api/com/...util/DateDiff.html
etc

quote:
Originally posted by UTI
No... cause you'd need to add in leap years and such.
I dunno exactly for Java, but in any other language I know Date objects are in milliseconds or at least in seconds or another form which you can substract from each other.

You do not need to take in account leap years, lead days and leap seconds. This is automatically done as the calenders of the objects use Gregorian calenders.

But as said, I dunno if this is also the case in Java (but I would be surprised if it wasn't)...
RE: Formula to calculate days lived? by RaceProUK on 11-12-2006 at 03:44 PM

quote:
Originally posted by CookieRevised
I dunno if this is also the case in Java
The Java Date object (and anything to do with dates) uses the Gregorian calendar, and will correctly deal with leap years.
RE: Formula to calculate days lived? by fluffy_lobster on 11-12-2006 at 08:18 PM

this is why dates need to go metric...


RE: Formula to calculate days lived? by RaceProUK on 11-12-2006 at 10:46 PM

quote:
Originally posted by fluffy_lobster
this is why dates need to go metric...
They'll never go metric, simply because it's not possible to define a metric system for dates that everyone understands. Look at Swatch's failed .beat system for instance.
RE: RE: Formula to calculate days lived? by rav0 on 11-13-2006 at 06:24 AM

quote:
Originally posted by fluffy_lobster
this is why dates need to go metric...

Yep, well, we need a decimal calendar (metric means based on the metre). Problem is, days and years are both determined by Earth's movement. You can't make up an arbitary unit like the metre, a year has 365 days (and a bit), that's just the way it is.
RE: Formula to calculate days lived? by Jarrod on 11-14-2006 at 10:58 AM

just use the counter psm script or the holiday counter have a look at how they did it