Skip to content

Instantly share code, notes, and snippets.

@andreymusth
Created May 26, 2020 07:52
Show Gist options
  • Save andreymusth/53fa227ae04830c48ebf7007fc360906 to your computer and use it in GitHub Desktop.
Save andreymusth/53fa227ae04830c48ebf7007fc360906 to your computer and use it in GitHub Desktop.
class DateFormatter {
private val cache = HashMap<Long, String>()
private val months = arrayOf(
"января",
"февраля",
"марта",
"апрель",
"мая",
"июня",
"июля",
"августа",
"сентября",
"октября",
"ноября",
"декабря"
)
// принимает long UNIX Time и возвращает месяц + год -> апреля 2024 года.
// т.е. нам интересно форматирвание в пределах месяца
// оптимизирована для частых вызовов
fun formatDate(time: Long): String {
if (cache.containsKey(time)) {
return cache[time]!!
}
val date = Date(time)
val formattedDate = getDate(date.year, date.month)
cache[time] = formattedDate
return formattedDate
}
private fun getDate(year: Int, month: Int): String {
val monthRu = months[month]
return monthRu + " " + year + " года"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment