Re: Date Difference - Qsn



Hal Rosser wrote:
"alij" <aliasger.jaffer@xxxxxxxxx> wrote in message news:1180383514.291807.156870@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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

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.)

--
Eric Sosman
esosman@xxxxxxxxxxxxxxxxxxx
.



Relevant Pages

  • Re: Avoiding the Leap Second
    ... 100 milliseconds "under the rug" by adding one millisecond to the last ... correct time, and set our clocks ... No leap seconds necessary. ... average length of the second plus occasional corrections that involved ...
    (sci.astro.amateur)
  • Re: Avoiding the Leap Second
    ... 100 milliseconds "under the rug" by adding one millisecond to the last ... correct time, and set our clocks ... No leap seconds necessary. ... The frequency offset meant that it was awkward to use the standard ...
    (sci.astro.amateur)
  • Re: Avoiding the Leap Second
    ... For the first 33 1/3 days of each of those parts, sweep increments of ... 100 milliseconds "under the rug" by adding one millisecond to the last ... any reason that civil time needs to be particularly accurate. ... No leap seconds necessary. ...
    (sci.astro.amateur)
  • Re: The Leap Millisecond
    ... from March 1 to keep things simple in leap years. ... we'd have an *enormous* number of leap milliseconds. ... For human-precision purposes, a leap second doesn't matter, ...
    (sci.astro.amateur)