Created
February 16, 2018 17:07
-
-
Save hernandazevedo/c71d9e4daeeefe2e2dcb6dbe37c336d8 to your computer and use it in GitHub Desktop.
This file contains 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
object TimeUtils { | |
fun millisToShortDHMS(duration: Long): String { | |
var res = "" // java.util.concurrent.TimeUnit; | |
val days = TimeUnit.MILLISECONDS.toDays(duration) | |
val hours = TimeUnit.MILLISECONDS.toHours(duration) - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(duration)) | |
val minutes = TimeUnit.MILLISECONDS.toMinutes(duration) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(duration)) | |
val seconds = TimeUnit.MILLISECONDS.toSeconds(duration) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(duration)) | |
val millis = TimeUnit.MILLISECONDS.toMillis(duration) - TimeUnit.SECONDS.toMillis(TimeUnit.MILLISECONDS.toSeconds(duration)) | |
if (days == 0L) | |
res = String.format("%02d:%02d:%02d.%04d", hours, minutes, seconds, millis) | |
else | |
res = String.format("%dd %02d:%02d:%02d.%04d", days, hours, minutes, seconds, millis) | |
return res | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment