如何将 1800 秒格式化为 MM:SS 格式

ARG*_*Geo 2 android countdowntimer kotlin

我有以下用于简单倒数计时器的 Kotlin 代码:

val thousand: Long = 1000

val timer = object: CountDownTimer(1800000, 1000) {

    override fun onTick(millisUntilFinished: Long) {
        var timeResult = millisUntilFinished/thousand
        textTimer.text = "$timeResult"
    }
    override fun onFinish() {
        textTimer.text = "Time is out"
    }
}
timer.start()
textTimer.text = "$timer"
Run Code Online (Sandbox Code Playgroud)

如何格式化1800 seconds30 min : 00 sec在科特林

for*_*pas 6

您可以通过整数除法和取模 ( %)获得数字,
并将格式设置为 2 位padStart()

val secs = 1800
val formatted = "${(secs / 60).toString().padStart(2, '0')} min : ${(secs % 60).toString().padStart(2, '0')} sec"
println(formatted)
Run Code Online (Sandbox Code Playgroud)

将打印

30 min : 00 sec
Run Code Online (Sandbox Code Playgroud)