I have an small problem.
I create an List with the day names of a week that I use for the title row of an month calendar.
But the problem is the first row of the calendar starts on different week days, given by the localization of the phone. For example USA on Sunday, Germany on Monday.
How do I archive this sorting?
fun getLocalizedShortWeekDays(): List<String> {
val locale = Locale.getDefault()
val listOfDayNames = DayOfWeek.entries.map { it.getDisplayName(TextStyle.SHORT, locale) }.toList()
val orderedListOfDayNames =
return listOfDayNames
}
I want to sort a List of day names in the order of the localization of the phone.
You can use WeekFields.getFirstDayOfWeek() and cycle through the entries:
fun getLocalizedShortWeekDays(): List<String> {
val locale = Locale.getDefault()
val listOfDayNames = DayOfWeek.entries.map { it.getDisplayName(TextStyle.SHORT, locale) }
val firstDayOfWeek = WeekFields.of(locale).firstDayOfWeek.ordinal
val orderedListOfDayNames =
listOfDayNames.subList(firstDayOfWeek, 7) + listOfDayNames.subList(0, firstDayOfWeek)
return orderedListOfDayNames
}