Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

claculating the number of years from two java.Util dates

Thread view: 
manzur - 13 Jun 2006 13:40 GMT
I have two java.util dates with me i want calculate the number of years
between them .

Example:

Date1:19/02/2006
Date2:19/02/2007

If i give these two dates i should get the number ofyears as 1.

thanks in advance
MiSt - 13 Jun 2006 14:29 GMT
manzur napisał(a):
> I have two java.util dates with me i want calculate the number of years
> between them .
[quoted text clipped - 5 lines]
>
> If i give these two dates i should get the number ofyears as 1.

It depends what do you want.
What if ?:

Date1=19/12/2006,
Date2=19/02/2007

It should give 1 year or  0 year?

If you want only calendar differnce (in my example 1 year) create two
Calendar objects and get years fields values and check diference

If you want duration  (in my example 0 year) get time in milis from date
 get differnce and divide by number of milis in year or simply use
DurationFormatUtils from apache commons lang

Signature

MiSt

Seamus - 13 Jun 2006 17:46 GMT
> I have two java.util dates with me i want calculate the number of years
> between them .
[quoted text clipped - 7 lines]
>
> thanks in advance

Given two Data objects, d1 and d2

long elapsed = d2.getTime() - d1.getTime();
// elapsed is in milliseconds so we have to convert to year
int years = elapsed * 1000 * 60 * 60 * 24 * 365; // leap years make it
more complex TEISTTR
P.Hill - 15 Jun 2006 04:05 GMT
> long elapsed = d2.getTime() - d1.getTime();
> // elapsed is in milliseconds so we have to convert to year
> int years = elapsed * 1000 * 60 * 60 * 24 * 365;

// leap years make it more complex TEISTTR

Oh and since we don't use a calendar with leaps (nor funny days with 23
and 24 hours) then it really isn't important.  Oh wait we do, so your
solution is pretty poor suggestion, particularly for those real world
calculations like right about the same time last year to right about the
same time this year.

Suggestion to OP, use the Calendar object and convert properly to
either calendar year and if you want more than (any day in ) 2006
minus (any day in) 2005 is 1, I'd suggest working with day of year
or month of year and day of month to work out "delta years" instead of
"calendar years".  All the fields I mentioned are available from
java.util.Calendar.

-Paul
Seamus - 20 Jun 2006 19:53 GMT
> > long elapsed = d2.getTime() - d1.getTime();
> > // elapsed is in milliseconds so we have to convert to year
[quoted text clipped - 16 lines]
>
> -Paul

Yes, and I mentioned that - smartass.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.