does kotlinx-datetime have a way to get a date from the weeknumber ?
i found a workaround using
java.util.Calendar
but i am not happy about it
Copy code
fun getMondayOfWeek(year: Int, week: Int): LocalDate {
val cal = Calendar.getInstance()
cal.set(Calendar.WEEK_OF_YEAR, week)
cal.set(Calendar.YEAR, year)
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY)
return cal.time.toInstant().toKotlinInstant().toLocalDateTime(systemTZ).date
}
is there a better way ?
i
ilya.gorbunov
04/23/2021, 8:56 AM
You mean the week number of ISO week calendar?
n
Nikky
04/23/2021, 12:52 PM
yes, what i want is just to get the date of eg. monday of week 1 of a year
all the rest can be solved with DateRange arithmetic
i
ilya.gorbunov
04/24/2021, 6:16 PM
We do not have convenient way of working with ISO week calendar.
If I recall correctly, the first week of year is the week with Thursday or, which is equivalent, the week with Jan 4. So in order to find the Monday of the first week, you can construct the date of (year, January, 4) and then scroll it to Monday: