Note that there are some explanatory texts on larger screens.

plurals
  1. POJava/Android absolute fastest way to periodically get current seconds (0-59) and/or milliseconds (0-999)
    primarykey
    data
    text
    <p>I'm executing a <code>Runnable</code> within an Android application every 100ms wrapped in a Java <code>ScheduledExecutorService</code>. As a part of the <code>run()</code> block I need to obtain the current seconds as 0-59 and milliseconds as 0-999. Both should be in local time, although UTC is workable too.</p> <p>What's the <strong>absolute fastest</strong> way to do this? Internationalization/unique calendars aren't required.</p> <p>Currently I'm using <code>Calendar</code> in the below way, but I was thinking it could be faster to use <code>System.currentTimeMillis()</code> and do some math, I'd appreciate any input.</p> <pre><code>Calendar calendar = Calendar.getInstance(); int seconds = calendar.get(Calendar.SECOND); </code></pre> <p>If it's relevant, this is actually within an Android application.</p> <p><strong>Edit for clarification</strong></p> <p>Obtaining the current seconds as 0-59 (i.e. relative to the current minute) is critical, so <code>System.currentTimeMillis()</code> comes with a math cost as I mentioned above. What I wasn't sure about was whether doing this math + the system call would be faster than the call to <code>Calendar</code>, or if there are any other options (like <a href="http://joda-time.sourceforge.net/" rel="nofollow">Joda Time</a>) which may have even less of a footprint. </p> <p>For reference, below is the most straightforward way I could come up with off the top of my head to get seconds (0-59) from the Unix timestamp.</p> <pre><code>double seconds = Math.floor(((Math.floor(System.currentTimeMillis() / 1000) % 86400) % 3600) % 60); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload