aspose file tools*
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 Soft Skills this week in the Jobs Discussion 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: 2615
    
    9

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 OCPBCD5
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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: 40052
    
  28
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: 2615
    
    9

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