year
The year component of the date.
Samples
import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.test.*
fun main() {
//sampleStart
// Formatting and parsing a date in complex scenarios
val format = DateTimeComponents.Format {
year(); char('-'); monthNumber(); char('-'); dayOfMonth()
}
val formattedDate = format.format {
setDate(LocalDate(2023, 1, 2))
check(year == 2023)
check(month == Month.JANUARY)
check(dayOfMonth == 2)
check(dayOfWeek == DayOfWeek.MONDAY)
}
check(formattedDate == "2023-01-02")
val parsedDate = format.parse("2023-01-02")
check(parsedDate.toLocalDate() == LocalDate(2023, 1, 2))
check(parsedDate.year == 2023)
check(parsedDate.month == Month.JANUARY)
check(parsedDate.dayOfMonth == 2)
check(parsedDate.dayOfWeek == null)
//sampleEnd
}