Created
February 24, 2017 10:53
-
-
Save sagrawal31/4ff18a4b3132ce1fc99b2bafb201001c to your computer and use it in GitHub Desktop.
A simple Java/Groovy code to convert a given seconds value to hh:mm:ss format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.concurrent.TimeUnit | |
void convert(int secondsToConvert) { | |
long millis = secondsToConvert * 1000; | |
long hours = TimeUnit.MILLISECONDS.toHours(millis); | |
long minutes = TimeUnit.MILLISECONDS.toMinutes(millis) % TimeUnit.HOURS.toMinutes(1); | |
long seconds = TimeUnit.MILLISECONDS.toSeconds(millis) % TimeUnit.MINUTES.toSeconds(1); | |
String format = String.format("%02d:%02d:%02d", Math.abs(hours), Math.abs(minutes), Math.abs(seconds)); | |
println("$secondsToConvert seconds = " + format); | |
} | |
convert(5) | |
convert(10) | |
convert(40) | |
convert(59) | |
convert(60) | |
convert(80) | |
convert(100) | |
convert(119) | |
convert(120) | |
convert(121) | |
convert(3600) | |
convert(8640) | |
convert(86400) | |
/* | |
* | |
5 seconds = 00:00:05 | |
10 seconds = 00:00:10 | |
40 seconds = 00:00:40 | |
59 seconds = 00:00:59 | |
60 seconds = 00:01:00 | |
80 seconds = 00:01:20 | |
100 seconds = 00:01:40 | |
119 seconds = 00:01:59 | |
120 seconds = 00:02:00 | |
121 seconds = 00:02:01 | |
3600 seconds = 01:00:00 | |
8640 seconds = 02:24:00 | |
86400 seconds = 24:00:00 | |
* | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment