Mark
09/10/2024, 2:06 PMLocalDate
without showing the year? Equivalent of Android’s DateUtils.FORMAT_NO_YEAR
Dmitry Khalanskiy [JB]
09/10/2024, 2:11 PMLocalDate.Format {
monthNumber(); char('-'); dayOfMonth()
}.format(LocalDate(2024, 5, 6))
Mark
09/10/2024, 2:36 PMDmitry Khalanskiy [JB]
09/10/2024, 2:56 PMMark
09/10/2024, 7:10 PMkevin.cianfarini
09/10/2024, 7:32 PMLocalDate
and then delegate formatting to the native platform, whether it’s Java, Apple, etc.Mark
09/11/2024, 8:36 AMinterface PlatformDateFormatter {
fun formatDate(date: LocalDate, includeYear: Boolean = true): String
fun formatDayOfWeek(dayOfWeek: DayOfWeek): String
}
class JavaDateFormatter(
private val locale: Locale,
): PlatformDateFormatter {
private val dateFormatWithoutYear = SimpleDateFormat("MMMM d", locale)
private val dateFormatWithYear by lazy {
SimpleDateFormat("MMMM d, yyyy", locale)
}
private val dayOfWeekFormatter = DateTimeFormatterBuilder()
.appendText(ChronoField.DAY_OF_WEEK, TextStyle.FULL)
.toFormatter(locale)
override fun formatDate(date: LocalDate, includeYear: Boolean): String {
return if (includeYear) {
dateFormatWithYear.format(date.millisSinceEpoch)
} else {
dateFormatWithoutYear.format(date.millisSinceEpoch)
}
}
override fun formatDayOfWeek(dayOfWeek: DayOfWeek): String {
return dayOfWeekFormatter.format(dayOfWeek)
}
}
val LocalDate.millisSinceEpoch: Long
get() = atStartOfDayIn(TimeZone.currentSystemDefault()).toEpochMilliseconds()