*
The moose likes Threads and Synchronization and the fly likes get time in microsecond precision Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "get time in microsecond precision" Watch "get time in microsecond precision" New topic
Author

get time in microsecond precision

Divya Kumar
Greenhorn

Joined: Mar 26, 2012
Posts: 16


Hi.I have to generate a time stamp in format of yyyyMMddHHmmsstttttt where tttttt is in microseconds.
I have tried nut as java don't support microsecond can any help me to solve this problem
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

As you already know, Java doesn't support microsecond precision. There's no way to do this with pure Java.

In fact, on most systems, you can't get the current time to that level of precision at all. It's simply not available. At least not directly.

The best you could do would be something like this:

Call System.currentTimeMillis() and System.nanoTime() one right after the other. currentTimeMillis() gets you the current time, but it will typically only be accurate to about 10 ms. nanoTime() can be accurate down to the nanosecond (although there's no guarantee it will even be any more accurate than currentTimeMillis()) but it's only useful for elapsed time between one call to nanoTime() and another.

Now that you have a baseline current time for nanoTime(), when you need to generate your timestamp, call nanoTime() compute the difference from the result you got when you called currentTimeMillis(), and build a current time from that.

However...

1) There's no guarantee you'll get microsecond accuracy.

2) Why do you even need current time down to the microsecond. Elapsed time I could see, but why current time?
Divya Kumar
Greenhorn

Joined: Mar 26, 2012
Posts: 16

Thanks Jeff
In API I notice one more thing that..System.nanoTime() take more CPU clocks than System.getmiliSeconds() method.
Also there is method that we call some native method and calculate the microseconds in C.
But I have a query why Java don't provide Current Time in microsecond precision.
Any idea???
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

divya kumar wrote:
In API I notice one more thing that..System.nanoTime() take more CPU clocks than System.getmiliSeconds() method.


I'm curious as to how you determined this.

Also there is method that we call some native method and calculate the microseconds in C.


And is it actually accurate to the microsecond? Just because it reports in microseconds doesn't mean that it's actually measuring with microsecond precision.

But I have a query why Java don't provide Current Time in microsecond precision.
Any idea???


As I understand it, the underlying API that currentTimeMillis() uses was generally not accurate to within more than 5-15 ms when Java first came out, and system clocks drift enough that you probably won't get reliable microsecond timing in any case.

As I pointed out, there is the nanoTime() method, but just like currentTimeMillis(), it depends on what the underlying OS provides.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2339
    
  28

Another thing to note about nanoTime, since nanoTime uses a lot more CPU cycles, if you use nanoTime to do performance analysis, you might see "ghosts" in your analysis. If you start profiling an application where the time differrence in terms of nano seconds is material, and you use nanoTime to profile the app, then you might run into a situation where the profiling code takes longer than the code. It's like the Schrodinger's cat. The mere act of trying to profile the application makes the application slower. You profile module A, and module A gets slower.. you profile module B and module B gets slower. You are better off profiling the app so that even if call nanoTime, the material differrence in time is in milliSeconds.

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

what's wrong with you all a microsecond seems like eternity to me


SCJP
Visit my download page
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: get time in microsecond precision