Reputation: 58493
I have the following Kotlin code for simple countdown timer:
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"
How to format 1800 seconds
to 30 min : 00 sec
in Kotlin?
Upvotes: 4
Views: 2946
Reputation: 58493
Here's how my code looks like at the moment:
val timer = object: CountDownTimer(1800000, 1000) {
override fun onTick(millisUntilFinished: Long) {
val timeResult =
"${(millisUntilFinished / 1000 / 60).toString().padStart(2, '0')}:" +
"${(millisUntilFinished / 1000 % 60).toString().padStart(2, '0')} "
textTimer.text = "$timeResult"
}
override fun onFinish() {
textTimer.text = "Time is out"
}
}
timer.start()
textTimer.text = "$timer"
Upvotes: 1
Reputation: 29864
Since you are targeting the JVM, you should use the Java 8 Date/Time Api. You could create a very concise function like this:
fun countdown(s: Long) = with(LocalTime.ofSecondOfDay(s)) {
String.format("%02d min :%02d sec", minute, second)
}
countdown(1800) // 30 min : 00 sec
Recommendation:
Don't do the calculations at call site (in onTick
). This makes the code unecessary hard to understand. Create a separate function for that.
It's a simple calculation, but use what the standard libraries give you. Firstly, the code is optimized and secondly you can easily extend the function to calculate hours and so on. Untested, hand-drafted code is error prone.
Upvotes: 3
Reputation: 2426
Following code can convert from milliseconds (which is what you have initially) to minutes and seconds:
val milliseconds: Long = 1800000
val minutes = milliseconds / 1000 / 60
val seconds = milliseconds / 1000 % 60
println("$minutes min : $seconds sec")
Output:
30 min : 0 sec
EDIT 1:
If you strictly need it from seconds to minutes and seconds, just remove the extra division by 1000.
The code for that will be:
val seconds: Long = 1800
val minutes = milliseconds / 60
val seconds = milliseconds % 60
println("$minutes min : $seconds sec")
The code in the original question had milliseconds in it that's why I mentioned it from Milliseconds.
EDIT 2:
Missed out the double digit formatting.
The answer by @forpas using .toString().padStart(2, '0')
will give the correct formatted output. So, the correct code would be:
val milliseconds: Long = 1800000
val minutes = milliseconds / 1000 / 60
val seconds = milliseconds / 1000 % 60
println("{$minutes.toString().padStart(2, '0')} min : {$seconds.toString().padStart(2, '0')} sec")
Output:
30 min : 00 sec
Upvotes: 0
Reputation: 164139
You get the numbers by integer division and modulo (%
)
and the formatting to 2 digits with padStart()
:
val secs = 1800
val formatted = "${(secs / 60).toString().padStart(2, '0')} min : ${(secs % 60).toString().padStart(2, '0')} sec"
println(formatted)
will print
30 min : 00 sec
Upvotes: 8