Kotlin with Copilot: Create a list of 3 letters for weekdays and weekends
Video: Kotlin with Copilot: Create a list of 3 letters for weekdays and weekends by Taught by Celeste AI - AI Coding Coach
Kotlin: Lists of Weekday and Weekend Abbreviations
listOf("Mon", "Tue", "Wed", "Thu", "Fri")andlistOf("Sat", "Sun"). The simplest puzzle in the series — useful for showing how Copilot reads paired comments.
This one's tiny: hardcode two lists. The interesting part is what Copilot infers from comment context — and how to do this correctly with java.time.DayOfWeek instead of hardcoded strings.
The Copilot prompt
// Create a list of 3-letter abbreviations for weekdays
val weekdays =
Copilot completes:
val weekdays = listOf("Mon", "Tue", "Wed", "Thu", "Fri")
Then continue:
// Create a list of 3-letter abbreviations for weekend days
val weekends =
Copilot generates:
val weekends = listOf("Sat", "Sun")
println("Weekdays: $weekdays")
println("Weekends: $weekends")
Walkthrough
listOf(...) is the standard immutable-list constructor. Five strings for weekdays, two for weekends. Done.
But hardcoded strings are fragile. They:
- Break for non-English locales.
- Don't match java.time.DayOfWeek enum names.
- Have to be re-typed for "Monday" full names, "M" single-letter, etc.
The richer answer uses java.time.
With java.time
import java.time.DayOfWeek
import java.time.format.TextStyle
import java.util.Locale
val (weekdays, weekends) = DayOfWeek.values().partition { it.value <= 5 }
val weekdayAbbrevs = weekdays.map { it.getDisplayName(TextStyle.SHORT, Locale.ENGLISH) }
val weekendAbbrevs = weekends.map { it.getDisplayName(TextStyle.SHORT, Locale.ENGLISH) }
println(weekdayAbbrevs) // [Mon, Tue, Wed, Thu, Fri]
println(weekendAbbrevs) // [Sat, Sun]
Three steps:
- Partition by ISO weekday. ISO defines Monday=1, Sunday=7. Days 1-5 are weekdays; 6-7 are weekend.
- Map to localized short name.
getDisplayName(TextStyle.SHORT, locale)produces "Mon", "Tue", etc. for English. - Use the result. Now they adapt to locale.
For French:
val french = DayOfWeek.MONDAY.getDisplayName(TextStyle.SHORT, Locale.FRENCH)
// "lun." (with trailing period — French convention)
For Japanese:
val japanese = DayOfWeek.MONDAY.getDisplayName(TextStyle.SHORT, Locale.JAPANESE)
// "月" (single character)
TextStyle options
TextStyle.FULL — "Monday"
TextStyle.SHORT — "Mon"
TextStyle.NARROW — "M"
There's also FULL_STANDALONE, SHORT_STANDALONE, NARROW_STANDALONE — used in some languages where the day name differs based on context (e.g., Slavic languages have nominative vs. genitive forms).
When hardcoded strings are fine
For small one-off scripts in English, listOf("Mon", "Tue", ...) is fine. The bug emerges only when the app is translated.
For anything user-facing — labels, calendar headers, scheduling UIs — always go through the locale-aware API. It's roughly the same amount of code, and it scales to every locale Java supports.
Filtering by weekday/weekend
import java.time.LocalDate
val today = LocalDate.now()
val isWeekend = today.dayOfWeek.value >= 6
println(if (isWeekend) "Weekend" else "Weekday")
// Or via a Set
val weekend = setOf(DayOfWeek.SATURDAY, DayOfWeek.SUNDAY)
val isWeekend2 = today.dayOfWeek in weekend
Set membership is more readable for "is this in the weekend group."
All days in order
val allDays = DayOfWeek.values().toList()
println(allDays)
// [MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY]
DayOfWeek.values() returns an Array<DayOfWeek> in ISO order. .toList() for a List.
For weeks-start-on-Sunday (US convention), reorder:
val usOrder = listOf(DayOfWeek.SUNDAY) + DayOfWeek.values().toList().dropLast(1)
// [SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY]
The reorder is locale-specific — WeekFields.firstDayOfWeek from java.time.temporal.WeekFields gives you the first day for a locale.
Common mistakes
Hardcoded for one locale. Breaks the moment users see another language.
Wrong abbreviations. "Tu" vs "Tue" — both seen in the wild. getDisplayName picks the official locale convention.
Mixing ISO and Apple weekday numbers. ISO: Monday=1. Apple: Sunday=1. Don't reuse magic numbers across systems.
Forgetting value <= 5 for weekday. Saturday is 6, Sunday is 7. Inclusive <= matters.
Treating 5 days as universal. Many cultures have 6-day workweeks; some have Friday-Saturday weekends (Middle East). For real schedule UIs, get the rule from a source — don't bake it in.
What's next
Episode 20: Tell Copilot to "secure" a Jetpack Compose app. A meta-puzzle on what "secure" means and what Copilot will produce when prompted vaguely.
Recap
listOf("Mon", ...) works for one-off scripts. For real apps, use DayOfWeek.values() + getDisplayName(TextStyle.SHORT, locale) to get locale-aware abbreviations. ISO ordering: Monday=1, Sunday=7; weekend is 6-7. TextStyle.FULL/SHORT/NARROW for full name / 3-letter / 1-letter forms.
Next episode: securing a Compose app.