Apaec Posted August 26, 2015 Posted August 26, 2015 public static String formatTimeDHMS(final long time) { final int sec = (int) (time / 1000), d = sec / 86400, h = sec / 3600 % 24, m = sec / 60 % 60, s = sec % 60; return (d < 10 ? "0" + d : d) + ":" + (h < 10 ? "0" + h : h) + ":" + (m < 10 ? "0" + m : m) + ":" + (s < 10 ? "0" + s : s); } where time is in milliseconds. Implementation: g.drawString("Runtime: " + formatTimeDHMS(timer.getElapsed()), 100,100); where g = graphics2d and timer is your timer from the start of the script and getelapsed is a method from your timer class returning the time elapsed in millis This ill be formatted as: 00:00:00:00. d: h: m: s Apa 3
Czar Posted August 26, 2015 Posted August 26, 2015 (edited) ahh the days code was all I needed, tysm ! Edited August 26, 2015 by Czar
Dark Magician Posted August 26, 2015 Posted August 26, 2015 How about Class: public class Timer { private Instant start; private static final String formatter = "%02d"; public Timer() { reset(); } public void reset() { start = Instant.now(); } public Duration duration() { return Duration.between(start, Instant.now()); } @Override public String toString() { Duration duration = duration(); return String.format(formatter, duration.toHours()) + " : " + String.format(formatter, duration.toMinutes() % 60) + " : " + String.format(formatter, duration.getSeconds() % 60); } } Setup: Timer t = new Timer(); Usage: g.drawString("Time: " + t.toString(), 55, 401); 1
Apaec Posted August 26, 2015 Author Posted August 26, 2015 How about Class: public class Timer { private Instant start; private static final String formatter = "%02d"; public Timer() { reset(); } public void reset() { start = Instant.now(); } public Duration duration() { return Duration.between(start, Instant.now()); } @Override public String toString() { Duration duration = duration(); return String.format(formatter, duration.toHours()) + " : " + String.format(formatter, duration.toMinutes() % 60) + " : " + String.format(formatter, duration.getSeconds() % 60); } } Setup: Timer t = new Timer(); Usage: g.drawString("Time: " + t.toString(), 55, 401); That works too i have my method also built into my timer class I dislike java's duration and instant classes tho. your method does not format days though, but that would be easy to add. apa 1
Dark Magician Posted August 26, 2015 Posted August 26, 2015 (edited) That works too i have my method also built into my timer class I dislike java's duration and instant classes tho. your method does not format days though, but that would be easy to add. apa Ah yes yes, think I will add that in now. Guess i will add: return String.format(formatter, duration.toDays()) + " : " + String.format(formatter, duration.toHours()) + " : " + String.format(formatter, duration.toMinutes() % 60) + " : " + String.format(formatter, duration.getSeconds() % 60); Edited August 26, 2015 by Dark Magician
taco shack Posted August 28, 2015 Posted August 28, 2015 in timer class public static String format(long milliSeconds) { long secs = milliSeconds / 1000L; return String.format("%02d:%02d:%02d:%02d", new Object[] { Long.valueOf(milliSeconds / (1000*60*60*24)) , Long.valueOf((secs / 3600L) % 24), Long.valueOf((secs % 3600L) / 60L), Long.valueOf(secs % 60L) }); } in main final long lngStartTime = System.currentTimeMillis(); on paint g.drawString(""+Timer.format(System.currentTimeMillis() - lngStartTime), X, Y)
Apaec Posted August 28, 2015 Author Posted August 28, 2015 in timer class public static String format(long milliSeconds) { long secs = milliSeconds / 1000L; return String.format("%02d:%02d:%02d:%02d", new Object[] { Long.valueOf(milliSeconds / (1000*60*60*24)) , Long.valueOf((secs / 3600L) % 24), Long.valueOf((secs % 3600L) / 60L), Long.valueOf(secs % 60L) }); } in main final long lngStartTime = System.currentTimeMillis(); on paint g.drawString(""+Timer.format(System.currentTimeMillis() - lngStartTime), X, Y) Defining your start time at the top of your code as you have suggested is a risky thing to do - it can sometimes return unexpected results apa