3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

kotlinで時間を扱うならDurationが便利

Posted at

時間をLongで表していると、単位が何なのかわからなくなって困ったことはありませんか?
そんな時は Duration を使うと便利です

Duration

    // 作成
    val duration = System.currentTimeMillis().milliseconds

    // Doubleで取得
    val hour = duration.inSeconds

    // Longで取得
    val milliSec = duration.toLongMilliseconds()

    // Intで一括取得
    duration.toComponents { days, hours, minutes, seconds, nanoseconds ->
        // 使う値
    }

これならもとの値が何であれば、同じ単位として使うことができます。便利ですね。

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?