day Of Week
The day-of-week component of the date.
Samples
import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.test.*
fun main() {
//sampleStart
// Formatting and parsing a date with the day of the week in complex scenarios
val formatWithDayOfWeek = DateTimeComponents.Format {
dayOfWeek(DayOfWeekNames.ENGLISH_ABBREVIATED)
char(' ')
date(LocalDate.Formats.ISO)
}
val formattedWithDayOfWeek = formatWithDayOfWeek.format {
setDate(LocalDate(2021, 3, 28))
check(dayOfWeek == DayOfWeek.SUNDAY) // `setDate` sets the day of the week automatically
}
check(formattedWithDayOfWeek == "Sun 2021-03-28")
val parsedWithDayOfWeek = formatWithDayOfWeek.parse("Sun 2021-03-28")
check(parsedWithDayOfWeek.toLocalDate() == LocalDate(2021, 3, 28))
check(parsedWithDayOfWeek.dayOfWeek == DayOfWeek.SUNDAY)
// Note: the day of the week is only parsed when it's present in the format
val formatWithoutDayOfWeek = DateTimeComponents.Format {
date(LocalDate.Formats.ISO)
}
val parsedWithoutDayOfWeek = formatWithoutDayOfWeek.parse("2021-03-28")
check(parsedWithoutDayOfWeek.dayOfWeek == null)
//sampleEnd
}