🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: java.sql.Date (homework)

#338
From: Daniel Becroft
Date: Thu, 01 Dec 2005 18:58
50 lines
2538 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?
> 
> Thank you in advance. :>

Do you need to have fractions of years? If not, then I would just do something like the following:

/* **************************************************************************** */
[ Untested ]

int adjustment = 0;

if now.GetMonth() < dateOfBirth.getMonth() then
    adjustment = -1;
if now.GetMonth == dateOfBirth.GetMonth() && now.GetDate() < dateOfBirth.getDate()
    adjustment = -1;

/* 
** If adjustment == 0, then the person has had their birthday this year, otherwise, we need to
** adjust for this.
*/
int age_in_years = now.GetYear() - dateOfBirth.GetYear() + adjustment;
/* **************************************************************************** */

This, personally, gives cleaner code. But that's just me.

-- 
Daniel Becroft
; =================================
"Real computer scientists don't comment their code. The identifiers are so long they can't afford
the disk space."

"Blue sparks and white smoke, the two most expensive components of any electrical system, and once
used up will cost a fortune to replace."
Technical Details
Message-ID: <xn0eai5udpqix000@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!pd7cy2so!pd7cy1no!shaw.ca!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!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: "Daniel Becroft" <djcbecroft@hotmail.com>
Subject: Re: java.sql.Date (homework)
Newsgroups: borland.public.jbuilder.java.language
References:  <438f392a@newsgroups.borland.com>
Organization: -
User-Agent: XanaNews/1.17.6.6
Message-ID: <xn0eai5udpqix000@newsgroups.borland.com>
X-Ref: newsgroups.borland.com ~XNS:000033D3
NNTP-Posting-Host: 61.68.83.166
Date: 1 Dec 2005 11:58:57 -0700
X-Trace: newsgroups.borland.com 1133467137 61.68.83.166 (1 Dec 2005 11:58:57 -0700)
Lines: 50
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!pd7cy2so!pd7cy1no!shaw.ca!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail