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.");
Cœur
  • 37,241
  • 25
  • 195
  • 267
Chinmay
  • 180
  • 1
  • 2
  • 13
  • 1
    How are you getting the value from the server? – strmstn Oct 15 '12 at 11:12
  • 3
    Date is a wrapper for the value you get from `System.currentTimeMillis();` can you try using that instead? – Peter Lawrey Oct 15 '12 at 11:13
  • And what JRE version on server? – user1516873 Oct 15 '12 at 11:15
  • @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. – Chinmay Oct 15 '12 at 11:16
  • 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 issue – Anton Oct 15 '12 at 11:16
  • Are you sure you not removing trailing zeros? – mishadoff Oct 15 '12 at 11:18
  • @user1516873 : 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. – Chinmay Oct 15 '12 at 11:19
  • @Anton : I've not done anything with the value while logging – Chinmay Oct 15 '12 at 11:21
  • 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 JVM – AlexR Oct 15 '12 at 11:22
  • 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. – Peter Lawrey Oct 15 '12 at 11:23
  • 1
    Post the **exact code** you use to show the value. – Marko Topolnik Oct 15 '12 at 11:24
  • You are going through quite a number of redundant steps in there, but I don't see the reason for your obsevered behavior. – Marko Topolnik Oct 15 '12 at 11:29
  • 2
    Try `String longTime = String.valueOf(System.currentTimeMillis());` – Marko Topolnik Oct 15 '12 at 11:34
  • @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. – Chinmay Oct 15 '12 at 11:37
  • 1
    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`. – Marko Topolnik Oct 15 '12 at 11:39

1 Answers1

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

DrColossos
  • 12,656
  • 3
  • 46
  • 67
Munesh
  • 1,509
  • 3
  • 20
  • 46
  • 5
    This doesn't quite explain why the same code would return different results when run on different machines, does it? – Ewald Oct 15 '12 at 12:20
  • 1
    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. – Munesh Oct 15 '12 at 12:58
  • 1
    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 :) – Ewald Oct 15 '12 at 13:31
  • 2
    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... – Chinmay Nov 01 '12 at 14:23