I need to set the Date in the material date picker. It requires Long to set the date. I tried to do it with:
val formatter = DateTimeFormatter.ofPattern(DAY_FORMAT_PATTERN)
val selectedDate = LocalDate.parse(viewModel.selectedDateStateFlow.value, formatter)
val selectedDateToLong = selectedDate.atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli()
val datePicker = MaterialDatePicker.Builder.datePicker()
.setTheme(R.style.DatePicker)
.setSelection(selectedDateToLong)
.build()
in this case date picker sets the date one day less. Is there a more accurate conversion option?
for example:
I/System.out: viewModel.selectedDateStateFlow.value = 30 Nov, 2021
I/System.out: selectedDate = 2021-11-30
I/System.out: selectedDateToLong = 1638219600000
I/System.out: MaterialDatePicker.todayInUtcMilliseconds() = 1638230400000
but date picker set "Nov 29, 2021"
the timezone on phone GMT+03:00 Moscow Standard Time
UPD
Compared the value of my conversion of the LocalDate into Long and MaterialDatePicker.todayInUtcMilliseconds(). Received different values. What is the mistake of my translation?
viewModel.selectedDateStateFlow.valueand reformatting the data in the data picker. It would be good if the example included the values ofviewModel.selectedDateStateFlow.value,selectedDate,selectedDateToLongand what the date picker shows.viewModel.selectedDateStateFlow.valueis already "one day less" than what you expect? Did you check its value?selectedDate- i get correct date. By Default it current date. timezone on phone GMT+03:00 Moscow Standard TimeMaterialDatePicker.todayInUtcMilliseconds(). Is that the same value asselectedDateToLong? If not, what is the numerical difference? Also, might as well tryZoneOffset.UTCinstead ofZoneId.systemDefault():-)