1

The application I'm working on is related to the weekly calendar.

I want to fetch the days of the week. The code below for this is fully working. But I have to lower the API level of the project to 26 -> 24. Thus, some codes become inoperable at API 24 level.

How can I go about this in an alternative way?

My Code:

fun daysOfWeekFromLocale(): Array<DayOfWeek> {

val firstDayOfWeek = WeekFields.of(Locale.getDefault()).firstDayOfWeek
var daysOfWeek = DayOfWeek.values()

if (firstDayOfWeek != DayOfWeek.MONDAY) {
    val rhs = daysOfWeek.sliceArray(firstDayOfWeek.ordinal..daysOfWeek.indices.last)
    val lhs = daysOfWeek.sliceArray(0 until firstDayOfWeek.ordinal)
    daysOfWeek = rhs + lhs
}

return daysOfWeek
}

Android studio's Red Lines:

Function Image

(In all cases this function should return the same value. -- Array< DayOfWeek > )

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154

1 Answers1

2

API “de-sugaring”

The latest Android tooling brings to earlier Android most of the java.time functionality built into Android 26+ via “API desugaring”.

Three-TenABP

If that does not work for you, use the ThreeTenABP library, an Android-specific adaptation of ThreeTen-Backport.


For more details, see How to use ThreeTenABP in Android Project. See Answers by kael and by me.

Table of which java.time library to use with which version of Java or Android

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154