jQuery in Action, 3rd edition
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Milliseconds from Current System Time" Watch "Milliseconds from Current System Time" New topic

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,
Rob Spoor

Joined: Oct 27, 2005
Posts: 20273

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.

How To Ask Questions How To Answer Questions
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

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 !
I agree. Here's the link: http://aspose.com/file-tools
subject: Milliseconds from Current System Time
It's not a secret anymore!