Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Millisecond accuracy

 
K. Tsang
Bartender
Posts: 3348
12
Android Java
  • Mark post as helpful
  • send pies
  • 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()?
 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • 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: 3348
12
Android Java
  • Mark post as helpful
  • send pies
  • 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 ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic