Skip to content

Instantly share code, notes, and snippets.

@Razeeman
Created July 31, 2019 07:58
Show Gist options
  • Save Razeeman/ad2cafb6784cd1f528c332697fa88024 to your computer and use it in GitHub Desktop.
Save Razeeman/ad2cafb6784cd1f528c332697fa88024 to your computer and use it in GitHub Desktop.
Format time in milliseconds to the string of the type "6 дней 23:59:59"
/**
* Format time in milliseconds to the string of the type "6 дней 23:59:59".
*
* @param millis number of milliseconds.
* @return formatted string.
*/
fun convertMillisecondsToDHMS(millis: Long): String {
val s = TimeUnit.MILLISECONDS.toSeconds(millis) % TimeUnit.MINUTES.toSeconds(1)
val m = TimeUnit.MILLISECONDS.toMinutes(millis) % TimeUnit.HOURS.toMinutes(1)
val h = TimeUnit.MILLISECONDS.toHours(millis) % TimeUnit.DAYS.toHours(1)
val d = TimeUnit.MILLISECONDS.toDays(millis).toInt()
val hms = String.format("%02d:%02d:%02d", h, m, s)
val days by lazy { resourceUtils.getResources().getQuantityString(R.plurals.days_plural, d, d) }
return if (d > 0) "$days $hms" else hms
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment