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
Watch full page →What is the exact day of the week of 2023-01-01?
Determining the day of the week for a specific date is a common task in programming. In Kotlin, you can easily find the exact day of the week for January 1, 2023, using the built-in date and time APIs. This example shows how to get the day of the week from a LocalDate object.
Code
import java.time.LocalDate
import java.time.DayOfWeek
fun main() {
// Create a LocalDate object for January 1, 2023
val date = LocalDate.of(2023, 1, 1)
// Get the day of the week for the given date
val dayOfWeek: DayOfWeek = date.dayOfWeek
// Print the day of the week
println("The day of the week for 2023-01-01 is $dayOfWeek")
}
Key Points
- Kotlin's java.time.LocalDate class represents a date without time or timezone.
- The dayOfWeek property returns a DayOfWeek enum indicating the exact weekday.
- LocalDate.of(year, month, day) creates a date instance for the specified date.
- Printing the DayOfWeek enum value shows the weekday name in uppercase (e.g., SUNDAY).