month Number
The number-of-month (1..12) component of the date.
Throws
during assignment if the value is outside the 0..99
range.
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
}