• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Millisecond accuracy

 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello hi, I am new to this forum. Currently playing with the Date and Calendar classes. When I come across getting the milliseconds since Jan 1, 1970, I found there is indeed some difference by 20ms!!



The output:
Current date/time: Sat Oct 06 00:30:22 GMT+08:00 2007
Cal Milliseconds since Thu Jan 01 08:00:00 GMT+08:00 1970: 1191601822049
Dt Milliseconds since Thu Jan 01 08:00:00 GMT+08:00 1970: 1191601822029

So my question is which method is more accurate - Calendar.getTimeInMillis() or Date.getTime()?
 
Marshal
Posts: 74347
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Swap the two print lines round and you will probably find it takes 20 milliseconds to print the answer!
 
Campbell Ritchie
Marshal
Posts: 74347
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have tried your little app and it seems more complicated than I had thought.
What matters is which order the two lines which create the objects are in. Create the "cal" object before "dt" and "calMilli" is slightly less; create "dt" before "cal" and "dtMilli" is slightly less.

That should provide enough information for you to work out what the real problem is.

[EDIT]Minor spelling correction. And welcome to the Ranch [/EDIT]
[ October 05, 2007: Message edited by: Campbell Ritchie ]
 
K. Tsang
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help. I found that both methods are in fact the same as long I put them in the same line.

[ October 06, 2007: Message edited by: K. Tsang ]
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic