https://kotlinlang.org logo
Title
c

Colton Idle

01/05/2021, 1:44 PM
Is there an easy way to use TimeUnit class to specify 30 minutes for example? I found myself doing
val thirty_mins = 30 * 60_000
but there has to be a way to just do TimeUnit.Minutes(30) right?
v

Vampire

01/05/2021, 1:49 PM
You mean like
TimeUnit.MILLISECONDS.convert(30, TimeUnit.MINUTES)
or
TimeUnit.MINUTES.toMillis(30))
?
Or
30.minutes.inMilliseconds
?
l

Lukas Sztefek

01/05/2021, 1:55 PM
Based on your example you probably looking for
TimeUnit.MINUTES.toMillis(30)
v

Vampire

01/05/2021, 1:57 PM
Whoups, ms, changed my examples
t

tseisel

01/05/2021, 3:48 PM
kotlin.Duration is so pleasant compared to TimeUnit, too bad it's still experimental
n

nanodeath

01/05/2021, 5:09 PM
if it's a JVM project I'd probably use the Java Duration class
v

Vampire

01/05/2021, 5:34 PM
Why don't you like
30.minutes.inMilliseconds
?
c

Colton Idle

01/05/2021, 6:56 PM
I didn't know that's a thing! Thanks!
m

maaxgr

01/06/2021, 6:42 AM
what's to reason why this returns a double?Seems not to be the correct type for milliseconds. The same for days and all other units
v

Vampire

01/06/2021, 12:09 PM
Why not?
1500.microseconds.inMilliseconds
5.hours.inDays