1

How does java.util.Date.getTime method convert a given date & time into long number?

Java API documents say that - "Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object."

Appreciate any help.

Ujjwal
  • 603
  • 12
  • 23

2 Answers2

3

Check out the Date.java source code.

You'll see that in the simplest case, the Date object stores the number of milliseconds since 1970, rather than the date/time etc.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
1

Actually, despite the apparently unambiguous definition in the Java API doc, it is interesting to note that the number of milliseconds reported is not the actual number of physical milliseconds, or seconds for that matter, that have elapsed since January 1st 1970 00:00:00 GMT. It is really the number of physical seconds plus the number of leap seconds that have been artificially inserted.

Marcus Junius Brutus
  • 26,087
  • 41
  • 189
  • 331
  • 1
    System.currentTimeMillis() officially relates to OS timer - see http://stackoverflow.com/questions/14065355/are-leap-seconds-catered-for-by-calendar, therefore it is likely that it doesn't reflect leap seconds. I tested this on Windows 7 and it DOES NOT include the leap seconds. – javaPhobic Jun 30 '15 at 02:15
  • @javaPhobic I believe we're saying the same thing: if the number of leap seconds is not "reflected" (i.e. not accounted for), then the value given is the length of actual elapsed time (i.e. the time one would get by using a stopwatch since January 1st 1970) plus the sum of leap seconds that have been inserted. – Marcus Junius Brutus Jun 30 '15 at 07:33
  • I thought it should be minus, i.e. the long return by getTime() is the actual milliseconds elapsed minus the sum of leap seconds that have been inserted. – javaPhobic Jul 01 '15 at 00:38