aspose file tools*
The moose likes Beginning Java and the fly likes Milliseconds from Current System Time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Milliseconds from Current System Time" Watch "Milliseconds from Current System Time" New topic
Author

Milliseconds from Current System Time

Jose Campana
Ranch Hand

Joined: May 28, 2007
Posts: 339
Good day to you all,

It's good to be back and posting again. Let me be as straightforward as I can with following Question:

How can I get the Milliseconds of the current System Time and capture it in a variable?

I'm attempting the following:



The preceding code Prints 14 every time, and first of all, I'd like to know Why?.
I'm expecting it to print a different number every time (supposedly a 4 digit number). A number that represents the milliseconds within the second.
How can I accomplish that?

Thanks in advance,
Sincerely,
Jose
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

Calendar.MILLISECOND is a constant you can use to retrieve the number of milliseconds:

This isn't necessary however, and your code contains a lot of code that can be removed.

1) Calendar has a method called getTimeInMillis(). Guess what that does
2) Calendar.getInstance() already returns a Calendar object for the current moment in time. No need to use a new Date object to set the time again.
3) you can already retrieve the number of milliseconds from a Date object using its getTime() method
4) System.currentTimeMillis()!

In fact, that last one is in fact used by all the others as well.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

That's a constant value which is used to indicate to a Calendar object that you want to find out its milliseconds value. Like this:
Jose Campana
Ranch Hand

Joined: May 28, 2007
Posts: 339
Thank you very much, both are Excellent Answers !

Paul, that made me understand how the constants really work in the simplest of manners, And thanks to you Rob, as always. I can now refine my code according to your suggestions. Nice !

Thank you guys !
 
 
subject: Milliseconds from Current System Time