• 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

How to get an Accurate Calendar Instance to milliseconds

 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I have the following code for getting the Calendar time upto the nanos.


The problem with it is that it does not refresh in the loop. throughout the loop the time remains the same.Following is the undesired output.



How to use the Calendar so in every loop it gives me the latest time upto the latest nanosecond accuracy.

Thanks.
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Buy a slower machine:

Your code gets 10 times the same time, up to one nano.
Higher precision isn't supported by the OS I guess.

But since the value is returned as int, the maximum can not exceed 999.
So the last 0-digits are meaningless.
 
Gul Khan
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well than I think it will work in Production Env
If I increase the loop to 100 than there is a change. the milliseconds jump by 20 after a while. i dont know y is it not uniform. any ideas?

Thanks for replying.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps you're using a sophisticated, multi-threaded OS.
You get a small timeslice, and get 40 operations done in it, all in the same nano-second.
Then the OS stops your threat, and calls another one, i.e. refreshing the clock, looking for new hardware connected, refreshing the screen.
After this work is done, 20 nanos are gone, and you get another timeslice, to continue your work.
 
Ranch Hand
Posts: 374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Simply put, Windows does not have millisecond-precision time clocks. If you're on a windows machine, you'll NEVER achieve (well, ok, until a really slick new version of windows comes along, anyway) millisecond precision times. Search the Sun Java site for millisecond bugs or questions if you don't believe me.

If you need to get distinct values such as for timestamp values in a database, you're best writing your own utility for it to ensure a new value with each call--something like this:



It may not be the most efficient (using the CURRENT TIMESTAMP feature is preferable in most cases), but it works on all platforms--even those that do not have millisecond level precision.
[ September 07, 2004: Message edited by: David Hibbs ]
 
Stefan Wagner
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh - sorry, I didn't recognize, that the nano-part was already part of the milli-format.
 
reply
    Bookmark Topic Watch Topic
  • New Topic