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 / May 2007

Tip: Looking for answers? Try searching our database.

Date Difference - Qsn

Thread view: 
alij - 28 May 2007 21:18 GMT
Hi,

I have two dates and I want to find the difference in years between
them. I noticed the Date class doesn't provide me with such a function
like datediff. Will appreciate any pointers how I can go about this.

Thanks,

Alij
Eric Sosman - 28 May 2007 22:25 GMT
> Hi,
>
> I have two dates and I want to find the difference in years between
> them. I noticed the Date class doesn't provide me with such a function
> like datediff. Will appreciate any pointers how I can go about this.

    The "difference in years" could mean a number of
different things, for example:

    - If T1 is 31-Dec-2007 23:59:59 and T2 is 01-Jan-2008
      00:00:01, are T1 and T2 a year apart (they occur in
      different calendar years), or about 0.0000000634 years
      apart (two seconds)?

    - Same situation, but assuming that the chronology wizards
      add a leap second 23:59:60 to the end of December 2007.

    - Same situation, but assuming that T1 is given in EDT
      (UTC-5) and T2 in UTC.

    - If T1 is 01-Jan-2008 00:00:00 UTC and T2 is 01-Jan-2009
      00:00:00 UTC, are they one year apart, or a year and a
      day apart, or a year and 18 hours (approximately) apart?

    - If T1 is the birthday of Iannis Xenakis (29-May-1922) and
      T2 is the date of his death (04-Feb-2001), how many years
      apart are T1 and T2?  Keep in mind that Xenakis was a Greek
      composer, so the dates are given according to the calendars
      used in Greece at the relevant times.  Greece switched from
      the Julian to the Gregorian calendar in 1923, so in that year
      15-Feb was followed by 01-Mar; 1923 was only 352 days long in
      Greece, about 3.56% shorter than most other years.  Does that
      affect your answer?

    In short, before you can get a useful answer you need to
clarify your question.  Exactly what do you mean by "the difference
in years" between two moments?

Signature

Eric Sosman
esosman@acm-dot-org.invalid

Hal Rosser - 29 May 2007 01:59 GMT
> Hi,
>
> I have two dates and I want to find the difference in years between
> them. I noticed the Date class doesn't provide me with such a function
> like datediff. Will appreciate any pointers how I can go about this.

Use your Yankee engineering and the getTime() method of the Date class
if you know how many milliseconds there are between two dates, you can
cipher it out...
Assuming the two dates are date1 and date2 you can try this...
long dateDiffms = date1.getTime() - date2.getTime()
double yrsDiff = dateDiffms / (1000 * 60 * 60 * 24 * 365);   // assuming its
not a leap year
now you can use the Math class to round yrsDiff or use Math.floor if you say
you're not 21 until after your 21st birthday.
a lot of the Date's methods are deprecated, so you may want to use the
GregorianCalendar class.
Eric Sosman - 29 May 2007 02:22 GMT
>> Hi,
>>
[quoted text clipped - 9 lines]
> double yrsDiff = dateDiffms / (1000 * 60 * 60 * 24 * 365);   // assuming its
> not a leap year

    Be careful!  `1000 * 60 * 60 * 24 * 365' is 1471228928
milliseconds, which is a little over seventeen days.  How
the years fly by as I grow older!

    (ObJavaQuestion: Explain why the "obviously correct"
calculation turns out so badly wrong, and suggest a fix.)

Signature

Eric Sosman
esosman@acm-dot-org.invalid

Hal Rosser - 29 May 2007 05:20 GMT
>>> Hi,
>>>
[quoted text clipped - 16 lines]
>     (ObJavaQuestion: Explain why the "obviously correct"
> calculation turns out so badly wrong, and suggest a fix.)

OOPs!! better add an "L" or ".0"  to the end of (at least one) of those
numbers in parens so the calculations won't be done with ints only, which is
too small to hold the results.
Good catch, Eric Sosman! - thanks  We need the years to slow down - they fly
by too fast already without any help.
Sorry OP -
Jani Tiainen - 29 May 2007 06:34 GMT
Hal Rosser kirjoitti:
>> Hi,
>>
[quoted text clipped - 13 lines]
> a lot of the Date's methods are deprecated, so you may want to use the
> GregorianCalendar class.

And what happens if you're having change to/from DST? You lose or gain
one extra hour...

I've been pretty successful with Joda-Time library
<http://joda-time.sourceforge.net/>.

Signature

Jani Tiainen



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.