Is there a way to access a fraction of a second of the system clock? (not millis() related)

edited December 2016 in How To...

Here is the line of code that I am currently using to display the current system time in hr:mn:sc format:

print( hour()+":"+minute()+":"+second(),"-> ");

I would like to display hr:mn:sc:HH where HH is hundredths of a second on the system clock. I am NOT looking for the elapsed time since the program began, which would use the millis() function.

Thanks.

Answers

  • edited December 2016

    Based on this SimpleDateFormat tutorial: O:-)
    http://docs.Oracle.com/javase/tutorial/i18n/format/simpleDateFormat.html

    import java.util.Date;
    import java.text.SimpleDateFormat;
    
    final String PATTERN = "H:mm:ss:SSS";
    final SimpleDateFormat formatter = new SimpleDateFormat(PATTERN);
    
    Date now = new Date();
    String result = formatter.format(now);
    
    println(result);
    exit();
    
  • This also answers another problem with the code in the question, and I have used similar not knowing about Java & now. In all the systems I have worked with it's regarded as unreliable in that at e.g. 20:59:59 you read the hours and mins, and then the time changes to 21:00:00, you read the seconds so now you have 20:59:00. You have to read the whole date and time from the system in 1 uninterruptable call. The Java call probably does that.

  • edited December 2016

    Yes the Java call does that. But I didn't know that the system clock even keeps milliseconds.
    Just remember one thing - if you have any questions regarding things other than graphics, then search first in Google for Java.

  • edited January 2017 Answer ✓

    Keep in mind that, depending on your application, displaying wall-clock time is fine (use SSS), but using wall-clock time for recording interval measurements is generally not recommended due to locales, daylight savings, etc. etc. etc. If you are recording your results as data separate from the display you may want to use millis() or System.nanoTime() -- for example see discussion:

Sign In or Register to comment.