Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

new Date().getTime() not returning timestamp in milliseconds

I've a class which is using java.util.Date class to create a date object and using getTime() to get current milliseconds.

I've seen in the Java documentation that getTime() returns the milliseconds, and the same case is on my machine.

I've one other server, when I am deploying my application on server, the same getTime() returns the timestamp in seconds.

e.g.

  • value on server: 1350054625
  • value on local: 1350054625000

I am wondering how this is possible, I tried the same code locally and again I got timestamp in milliseconds.

Below is the part of code...

String longTime = new Long((new Date().getTime())).toString();
if(log.isDebugEnabled())log.debug("LAST_FEED_TIME will be " + longTime + " stored.");
like image 553
Chinmay Avatar asked Oct 15 '12 11:10

Chinmay


People also ask

What is new Date () getTime ()?

The date. getTime() method is used to return the number of milliseconds since 1 January 1970. when a new Date object is created it stores the date and time data when it is created. When the getTime() method is called on this date object it returns the number of milliseconds since 1 January 1970 (Unix Epoch).

Is new Date () getTime () UTC?

Use the getTime() method to get a UTC timestamp, e.g. new Date(). getTime() . The method returns the number of milliseconds since the Unix Epoch and always uses UTC for time representation. Calling the method from any time zone returns the same UTC timestamp.

What does getTime () mean?

getTime() The getTime() method returns the number of milliseconds since the ECMAScript epoch. You can use this method to help assign a date and time to another Date object. This method is functionally equivalent to the valueOf() method.

What does getTime () do in Java?

getTime. Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.


1 Answers

'new Date()' in turn uses System.currentTimeMillis()

System.currentTimeMillis

Returns the current time in milliseconds. Note that while the unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds.

See the description of the class Date for a discussion of slight discrepancies that may arise between "computer time" and coordinated universal time (UTC).

source: http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/System.html#currentTimeMillis()

like image 179
Munesh Avatar answered Oct 12 '22 21:10

Munesh