🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #335

Re: java.sql.Date (homework)

#335
From: Kevin Dean [TeamB]
Date: Thu, 01 Dec 2005 17:51
71 lines
3722 bytes
Petar Popara wrote:

>I have a date of birth as:
>
>java.sql.Date dateOfBirth = ...; // get from DB
>
>I have java.util.Date as current time:
>
>java.util.Date now = new java.util.Date();
>
>I need to calculate how many years is someone old. I have tried this:
>
>long oldnessInMillis = now.getTime() - dateOfBirth.getTime();
>long oldnessInYears = (oldnessInMillis / (1000 * 60 * 60 * 24 * 365));
>
>but I got unexpected results (bigger 21.5 times). Is it because of time 
>zones or I have miscalculated it?

Ah yes, fun with integers.

The problem is that the compiler, when it does the arithmetic with the 
numbers you have there, does it using integers by default.  That's fine 
right up until it gets to the last multiplication:

1000
*60 = 60,000
*60 = 3,600,000
*24 = 86,400,000
*365 = 31,536,000,000

To you and me, that looks fine, but the compiler is working with Java 
integers, which are represented in four bytes and therefore have a limited 
range (-2^31 to +(2^31-1) or -2,147,483,648 to +2,147,483,647 to be 
exact).  When you overflow an integer like that, the compiler just wraps 
around to the beginning again.

The final number actually ends up wrapping around the integer limit 7 
times, with a remainder of 1,471,228,928 or 21.44 times less than the 
number itself, hence your problem.

A better way to do this would be to force one of the constants to be a 
long integer, which will in turn force the compiler to do the calculation 
using long integers.  Keep in mind that the compiler will do so only at 
the point where you coerce one of the constants to a long integer, so the 
arithmetic up to that point will still be done with regular integers.

The recommended modification would be:

long oldnessInYears = (oldnessInMillis / (1000L * 60 * 60 * 24 * 365));

See the link below for a good discussion of Java data types and their 
limits.

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html

Now, if you want the calculation to be really accurate, you need to take 
leap years into consideration (every four years, except every hundred, 
except every four hundred).  I'll leave that as an exercise.  ;-)

-- 
Kevin Dean [TeamB]
Dolphin Data Development Ltd.
http://www.datadevelopment.com/

NEW WHITEPAPERS
Team Development with JBuilder and Borland Enterprise Server
Securing Borland Enterprise Server
http://www.datadevelopment.com/papers/index.html

Please see Borland's newsgroup guidelines at
http://info.borland.com/newsgroups/guide.html
Technical Details
Message-ID: <xn0eah3m9d9b9r000-kdean@www.teamb.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!npeer.de.kpn-eurorings.net!border2.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!pe2.news.blueyonder.co.uk!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail
References: <438f392a@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: f74b3,6ccfb3de53be2ae
X-Google-Attributes: gidf74b3,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Kevin Dean [TeamB]" <NkOdSePaAnM@datadevelopment.com>
Subject: Re: java.sql.Date (homework)
Newsgroups: borland.public.jbuilder.java.language
References:  <438f392a@newsgroups.borland.com>
Organization: Dolphin Data Development Ltd.
User-Agent: XanaNews/1.17.6.6
X-Face: ":yn4zt^A_dn>6~{YJmu^B\'4w)e:apPM\VC=Dvf&*tASxX9|BYczkFd$xr.$vNSZ[3Uobrp]c4\+\l){!WQI.$]veP({HLd;CDYH?iN
Message-ID: <xn0eah3m9d9b9r000-kdean@www.teamb.com>
X-Ref: newsgroups.borland.com ~XNS:000003CD
MIME-Version: 1.0
Content-Type: text/plain; format=flowed
NNTP-Posting-Host: 199.243.78.163
Date: 1 Dec 2005 10:51:52 -0700
X-Trace: newsgroups.borland.com 1133463112 199.243.78.163 (1 Dec 2005 10:51:52 -0700)
Lines: 71
X-Authenticated-User: kdean
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!npeer.de.kpn-eurorings.net!border2.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!pe2.news.blueyonder.co.uk!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail