What is the exact day of the week of 2023-01-01?
Video: What is the exact day of the week of 2023-01-01? by Taught by Celeste AI - AI Coding Coach
Kotlin: What Day of the Week is 2023-01-01?
LocalDate.of(2023, 1, 1).dayOfWeek— answer isSUNDAY. Two lines ofjava.timeinstead of any old-school calendar arithmetic.
A small puzzle that shows the modern java.time API at its cleanest. No magic numbers, no manual subtraction from a known date — just typed enum.
The Copilot prompt
import java.time.LocalDate
import java.time.DayOfWeek
// Print the day of the week for 2023-01-01
fun main() {
Copilot generates:
fun main() {
val date = LocalDate.of(2023, 1, 1)
val dayOfWeek: DayOfWeek = date.dayOfWeek
println("The day of the week for 2023-01-01 is $dayOfWeek")
}
Output:
The day of the week for 2023-01-01 is SUNDAY
Walkthrough
LocalDate.of(year, month, day) constructs a date. .dayOfWeek is a property that returns a DayOfWeek enum.
DayOfWeek has seven values, ISO 8601 ordering: MONDAY(1), TUESDAY(2), ..., SUNDAY(7). The toString() is the uppercase name.
For human-friendly output:
import java.time.format.TextStyle
import java.util.Locale
val dayName = date.dayOfWeek.getDisplayName(TextStyle.FULL, Locale.ENGLISH)
println(dayName) // "Sunday"
val short = date.dayOfWeek.getDisplayName(TextStyle.SHORT, Locale.ENGLISH)
println(short) // "Sun"
TextStyle options: FULL, SHORT, NARROW (and standalone variants). Locale controls the language: Locale.JAPANESE produces Japanese day names.
Comparing days
val today = LocalDate.now()
if (today.dayOfWeek == DayOfWeek.FRIDAY) {
println("It's Friday!")
}
if (today.dayOfWeek == DayOfWeek.SATURDAY || today.dayOfWeek == DayOfWeek.SUNDAY) {
println("Weekend")
}
DayOfWeek is an enum, so == works. There's also a convenient enum set:
val weekend = setOf(DayOfWeek.SATURDAY, DayOfWeek.SUNDAY)
if (today.dayOfWeek in weekend) println("Weekend")
Doing math with days
// Add days
val nextWeek = today.plusDays(7)
// Find next Monday
import java.time.temporal.TemporalAdjusters
val nextMonday = today.with(TemporalAdjusters.next(DayOfWeek.MONDAY))
// Find previous Friday
val prevFriday = today.with(TemporalAdjusters.previous(DayOfWeek.FRIDAY))
// First/last day-of-week of the month
val firstMondayOfMonth = today.with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY))
val lastFridayOfMonth = today.with(TemporalAdjusters.lastInMonth(DayOfWeek.FRIDAY))
TemporalAdjusters is a small library of common date queries. Cleaner than manual loops.
Numeric day value
If you really need the integer:
val ordinal = date.dayOfWeek.value // ISO: 1 (Monday) to 7 (Sunday)
value is the ISO-defined number: Monday=1, Sunday=7. Different from Apple's calendar (1=Sunday) and from java.util.Calendar (1=Sunday). Always be explicit when interfacing across systems.
DayOfWeek arithmetic
val today = DayOfWeek.WEDNESDAY
val twoLater = today.plus(2) // FRIDAY
val twoEarlier = today.minus(2) // MONDAY
// Wraps around
val sunday = DayOfWeek.SATURDAY.plus(1) // SUNDAY
val monday = DayOfWeek.SUNDAY.plus(1) // MONDAY (wraps)
plus/minus on DayOfWeek does modular arithmetic — no negative or out-of-range values.
Counting weekdays
import java.time.temporal.ChronoUnit
fun weekdaysBetween(start: LocalDate, endExclusive: LocalDate): Long {
val days = ChronoUnit.DAYS.between(start, endExclusive)
// approximate: assume 5/7 of all days are weekdays
// ... real version walks the dates
return (0 until days).count { i ->
start.plusDays(i).dayOfWeek.let { it != DayOfWeek.SATURDAY && it != DayOfWeek.SUNDAY }
}.toLong()
}
For accuracy, walk every day. For very long ranges (years), there's a closed-form math: 5 * (full_weeks) + remainder_weekdays. Use whichever fits.
Steering Copilot
If Copilot suggests Calendar.DAY_OF_WEEK (legacy):
// Use java.time.LocalDate, not java.util.Calendar
Naming the modern API in the comment is enough.
In Compose
@Composable
fun TodayLabel() {
val today = remember { LocalDate.now() }
val dayName = remember(today) {
today.dayOfWeek.getDisplayName(TextStyle.FULL, Locale.ENGLISH)
}
Text("Today is $dayName")
}
remember(today) re-runs the formatting only if today changes (which is never in this composition, but the explicit key documents the intent).
Common mistakes
Mixing ISO and Apple-calendar weekday numbers. ISO: Monday=1, Sunday=7. Apple/JS: Sunday=1, Saturday=7. Always check.
Using Calendar.DAY_OF_WEEK and getting weird numbers. Pre-Java-8 Calendar returns 1-7 with Sunday=1. Off-by-one when ported.
Hardcoding "Sunday" for display. Locale-specific. Use getDisplayName(TextStyle.FULL, locale).
DayOfWeek.toString() for the UI. Gives "SUNDAY" (uppercase). For "Sunday," use getDisplayName.
Comparing dayOfWeek to integers. if (date.dayOfWeek == 7) won't compile. Compare to DayOfWeek.SUNDAY.
What's next
Episode 13: Find two numbers in a list that add up to a target. The classic two-sum interview problem, with a hashmap O(n) solution.
Recap
LocalDate.of(y, m, d).dayOfWeek returns a DayOfWeek enum. ISO ordering: Monday=1, Sunday=7. For UI strings, getDisplayName(TextStyle.FULL, locale). TemporalAdjusters for "next Monday," "first Friday of month," etc. plus/minus on DayOfWeek wraps around the week.
Next episode: two-sum.