You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android API 24下替代java.time类实现本地化星期数组的方案咨询

适配API 24的本地化星期数组解决方案

Got it, let's sort this out. The core issue here is that DayOfWeek and WeekFields were both added in Android API 26, so they're not available when targeting API 24. But we can replicate the exact same logic using the older, widely supported Calendar class instead.

替代方案1:返回Calendar常量数组(直接适配)

If you can work with an IntArray using Calendar's built-in week constants (like Calendar.SUNDAY, Calendar.MONDAY), this is the simplest approach:

fun daysOfWeekFromLocale(): IntArray {
    val localCalendar = Calendar.getInstance(Locale.getDefault())
    val firstDayOfWeek = localCalendar.firstDayOfWeek

    // Default order matches Calendar's native sequence: SUNDAY to SATURDAY
    val defaultWeekDays = intArrayOf(
        Calendar.SUNDAY,
        Calendar.MONDAY,
        Calendar.TUESDAY,
        Calendar.WEDNESDAY,
        Calendar.THURSDAY,
        Calendar.FRIDAY,
        Calendar.SATURDAY
    )

    // Reorder if the first day isn't MONDAY (matches your original logic)
    if (firstDayOfWeek != Calendar.MONDAY) {
        val startIndex = defaultWeekDays.indexOf(firstDayOfWeek)
        val rhs = defaultWeekDays.sliceArray(startIndex until defaultWeekDays.size)
        val lhs = defaultWeekDays.sliceArray(0 until startIndex)
        return rhs + lhs
    }

    return defaultWeekDays
}

替代方案2:自定义枚举类(保持类似DayOfWeek的可读性)

If you want to keep a type-safe enum like DayOfWeek for code clarity, you can create your own enumeration that maps to Calendar constants:

// Custom enum to mimic API 26's DayOfWeek, compatible with API 24
enum class LocalDayOfWeek(val calendarValue: Int) {
    SUNDAY(Calendar.SUNDAY),
    MONDAY(Calendar.MONDAY),
    TUESDAY(Calendar.TUESDAY),
    WEDNESDAY(Calendar.WEDNESDAY),
    THURSDAY(Calendar.THURSDAY),
    FRIDAY(Calendar.FRIDAY),
    SATURDAY(Calendar.SATURDAY);

    companion object {
        fun fromCalendarValue(value: Int): LocalDayOfWeek =
            values().first { it.calendarValue == value }
    }
}

fun daysOfWeekFromLocale(): Array<LocalDayOfWeek> {
    val localCalendar = Calendar.getInstance(Locale.getDefault())
    val firstDayOfWeek = localCalendar.firstDayOfWeek
    val defaultWeekDays = LocalDayOfWeek.values()
    val startIndex = defaultWeekDays.indexOfFirst { it.calendarValue == firstDayOfWeek }

    if (firstDayOfWeek != Calendar.MONDAY) {
        val rhs = defaultWeekDays.sliceArray(startIndex until defaultWeekDays.size)
        val lhs = defaultWeekDays.sliceArray(0 until startIndex)
        return rhs + lhs
    }

    return defaultWeekDays
}

逻辑说明

Both solutions follow your original code's logic exactly:

  1. Grab the local calendar instance to get the region's preferred first day of the week
  2. Reorder the week days array so it starts with that preferred first day
  3. Return the adjusted array

This will work reliably on API 24 and above, and behaves identically to your original API 26 code.

内容的提问来源于stack exchange,提问作者Cemal Tüysüz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 22:07:48