This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have David & Dawn Griffiths on-line!
See this thread for details.
Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Milliseconds from Current System Time

 
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22509
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 26912
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic