• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

get time in microsecond precision

 
Greenhorn
Posts: 16
IntelliJ IDE Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
IntelliJ IDE Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what's wrong with you all a microsecond seems like eternity to me
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic