0
2

More than 3 years have passed since last update.

Kotlinで◯日前という表記をスマートに実装

Posted at

Kotlinで時間差を表示したい

Twitterなどで見る、「◯時間前」とか「◯日前」などの表記を簡単に実装したいと思います。
ちなみにこの方法なら、スマホの言語設定に合わせて勝手にローカライズされ、「◯ days ago」といった表記に変わりますので、配信する地域を気にせず使えます。

var hoge = DateUtils.getRelativeTimeSpanString(指定時刻のミリ秒, 現在時刻のミリ秒, 0)

で◯日前、◯時間前といった表記を取得できます。

現在時刻のミリ秒を取得

ちなみに現在時刻のミリ秒は下記で取得します。

val now_milisec = System.currentTimeMillis()
0
2
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
0
2