Kotlin
Rozwiązanie @Choletski, ale z sekundami iw Kotlinie
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()
}
}
Możesz przejść tutaj, date.getTime()
aby uzyskać wyniki takie jak
Today at 18:34:45
Yesterday at 12:30:00
Tomorrow at 09:04:05