Kotlin
@Choletskiソリューションですが、数秒でKotlinにあります
fun getMyPrettyDate(neededTimeMilis: Long): String? {
val nowTime = Calendar.getInstance()
val neededTime = Calendar.getInstance()
neededTime.timeInMillis = neededTimeMilis
return if (neededTime[Calendar.YEAR] == nowTime[Calendar.YEAR]) {
if (neededTime[Calendar.MONTH] == nowTime[Calendar.MONTH]) {
if (neededTime[Calendar.DATE] - nowTime[Calendar.DATE] == 1) {
"Tomorrow at " + DateFormat.format("HH:mm:ss", neededTime)
} else if (nowTime[Calendar.DATE] == neededTime[Calendar.DATE]) {
"Today at " + DateFormat.format("HH:mm:ss", neededTime)
} else if (nowTime[Calendar.DATE] - neededTime[Calendar.DATE] == 1) {
"Yesterday at " + DateFormat.format("HH:mm:ss", neededTime)
} else {
DateFormat.format("MMMM d, HH:mm:ss", neededTime).toString()
}
} else {
DateFormat.format("MMMM d, HH:mm:ss", neededTime).toString()
}
} else {
DateFormat.format("MMMM dd yyyy, HH:mm:ss", neededTime).toString()
}
}
ここdate.getTime()
を通過して、次のような出力を取得できます
Today at 18:34:45
Yesterday at 12:30:00
Tomorrow at 09:04:05