Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Milliseconds from Current System Time

 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Sheriff
Pie
Posts: 20958
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic