new Date().getTime() not returning timestamp in milliseconds
Asked Answered
E

1

9

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.");
Evonevonne answered 15/10, 2012 at 11:9 Comment(15)
How are you getting the value from the server?Androw
Date is a wrapper for the value you get from System.currentTimeMillis(); can you try using that instead?Riot
And what JRE version on server?Gabfest
@PeterLawrey : yes i've tried the same, it's working fine for me but i am looking in the direction that why the getTime() method is behaving like this.Evonevonne
I would say that string in logs files is trimmed somehow or you missed something. I don't think that there could be JVM/OS issueDuer
Are you sure you not removing trailing zeros?Aniseed
@Gabfest : i don't know the configuration. i just have access to a directory where i am uploading my files via ftp, i know 1 thing that it's unix server and locally i am running on windows.Evonevonne
@Duer : I've not done anything with the value while loggingEvonevonne
OS should not be relevant here. Try to implement simple command line app that prints new Date().getTime() and run it on your server with the same JVMMonomer
If the System.currentTimeMillis() is correct but the Date is wrong then the value used to construct the Date was also incorrect. I.e. the bug is not in Date but how it was constructed.Riot
Post the exact code you use to show the value.Maimonides
You are going through quite a number of redundant steps in there, but I don't see the reason for your obsevered behavior.Maimonides
Try String longTime = String.valueOf(System.currentTimeMillis());Maimonides
@MarkoTopolnik : yes, System.currentTimeMillis() is working fine for me but i am looking in the direction that why the getTime() method is behaving like this.Evonevonne
If you are positive that replacing with the code I posted changes the behavior on the server, then the only option I can see is that you are sucking in a broken implementation of java.util.Date at the server. This boils down to the server-side JRE or, theoretically but not very likely, to another JAR on the classpath that (should I say maliciously?) defines a broken java.util.Date.Maimonides
W
9

'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()

Wares answered 15/10, 2012 at 11:37 Comment(4)
This doesn't quite explain why the same code would return different results when run on different machines, does it?Armandinaarmando
Yes, This doesn't answer the question. But I thought this information would help in finding the issue. Ex : "Some operating systems measure time in units of tens of milliseconds." The unit of time measurement as per OS in the server can be second.Wares
That's true - but then Java should return it in the same format, but the milliseconds set to '0'. I understand why you placed the information, I was just checking to see if I'm missing something :)Armandinaarmando
I've changed code to System.currentTimeMillis() and my problem is solved, still i am looking for solution to this case, or an exact reason for why api is behaving in this way...Evonevonne

© 2022 - 2024 — McMap. All rights reserved.