wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Millisecond accuracy Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Millisecond accuracy" Watch "Millisecond accuracy" New topic
Author

Millisecond accuracy

K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2229
    
    7

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()?


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
Swap the two print lines round and you will probably find it takes 20 milliseconds to print the answer!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
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

Joined: Sep 13, 2007
Posts: 2229
    
    7

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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Millisecond accuracy
 
Similar Threads
Calendar() / Date() bug ???
Doubt in Data
10000 milliseconds showing as 01:00:10.000 - why ?
Woa! Why does Date/Calendar do this?
Date/Time display problem