LocalIsoWeekDate
The date part of a LocalDateTime, represented using the ISO week calendar.
This class is similar to the far more commonly used LocalDate, but uses a different calendar system. Here, every year begins on a Monday, and instead of the year being split into 12 months, each with 28-31 days, the week number and the day of the week are used to identify a specific date within the year.
More specifically, a LocalIsoWeekDate consists of three components:
A week year. In most cases, it is equal to the commonly used ISO year. However, the beginning and the end of the year can be different from LocalDate.year by several days, to ensure the week-year begins on a Monday.
A week number. The number of the week in the given year, starting with 1. Depending on the year, there can be at most 52 or 53 weeks.
The day of the week, from DayOfWeek.MONDAY to DayOfWeek.SUNDAY.
The range of supported dates is exactly the same as that of LocalDate. It is at least enough to represent dates of all instants between Instant.DISTANT_PAST and Instant.DISTANT_FUTURE.
Construction, serialization, and deserialization
LocalIsoWeekDate can be constructed directly from its components using the constructor. See sample 1.
A non-throwing version of the constructor is the orNull function.
toLocalDate and LocalDate.toLocalIsoWeekDate can be used to convert between LocalDate and LocalIsoWeekDate. See sample 2.
parse and toString methods can be used to obtain a LocalIsoWeekDate from and convert it to a string in the ISO 8601 extended format (for example, 2020-W01-1). See sample 3.
Samples
import kotlinx.datetime.*
import kotlin.test.*
fun main() {
//sampleStart
// Constructing a LocalIsoWeekDate corresponding to the date 2005-01-01
val isoWeekDate = LocalIsoWeekDate(2004, 53, DayOfWeek.SATURDAY)
check(2004 == isoWeekDate.isoWeekYear) // note: not 2005!
check(53 == isoWeekDate.isoWeekNumber)
check(DayOfWeek.SATURDAY == isoWeekDate.dayOfWeek)
//sampleEnd
}import kotlinx.datetime.*
import kotlin.test.*
fun main() {
//sampleStart
// Converting a LocalDate to a LocalIsoWeekDate and back
val date = LocalDate(2005, 1, 1)
val isoWeekDate = LocalIsoWeekDate(2004, 53, DayOfWeek.SATURDAY)
check(date == isoWeekDate.toLocalDate())
check(isoWeekDate == date.toLocalIsoWeekDate())
//sampleEnd
}import kotlinx.datetime.*
import kotlin.test.*
fun main() {
//sampleStart
// Parsing a LocalIsoWeekDate from a string corresponding to the date 2005-01-01
val isoWeekDateString = "2004-W53-6"
val parsed = LocalIsoWeekDate.parse(isoWeekDateString)
check(parsed == LocalIsoWeekDate(2004, 53, DayOfWeek.SATURDAY))
check(LocalDate(2005, 1, 1) == parsed.toLocalDate())
check(isoWeekDateString == parsed.toString())
//sampleEnd
}Constructors
Constructs a LocalIsoWeekDate instance from the given date components.
Constructs a LocalIsoWeekDate instance from the given date components.
Properties
The ISO week number within the week year.
The week-year part of the week-date. Not to be confused with the LocalDate.year!
Functions
Compares this LocalIsoWeekDate to another one. Returns zero if the two LocalIsoWeekDates represent the same date, a negative number if this date is earlier than the other, and a positive number if this date is later than the other.
Converts this LocalIsoWeekDate to a LocalDate.
Converts this LocalIsoWeekDate to a string in the ISO 8601 week date format (for example, 2023-W42-3).