Botre Posted May 6, 2015 Share Posted May 6, 2015 (edited) Because 500 timer snippets just isn't enough already. Uses the Duration class introduced in Java 8. Implementation example: Timer t = new Timer(); t.duration().getSeconds(); Source: import java.time.Duration; import java.time.Instant; /** * Created by Bjorn on 6/05/2015. */ 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); } } Duration class tease: Edited May 6, 2015 by Botre 2 Quote Link to comment Share on other sites More sharing options...
Fruity Posted May 6, 2015 Share Posted May 6, 2015 (edited) Amazing. Edited June 27, 2016 by FruityBug Quote Link to comment Share on other sites More sharing options...
Santonio Posted June 27, 2016 Share Posted June 27, 2016 Cool. Quote Link to comment Share on other sites More sharing options...