3

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?

6
  • I don't know too much about the Android APIs, but I would have expected this to work... Can you tell me what your system timezone is? Is it UTC+X? Commented Nov 30, 2021 at 10:24
  • 2
    Can you provide a concrete example? I'd assume there's a timezone difference somewhere between parsing viewModel.selectedDateStateFlow.value and reformatting the data in the data picker. It would be good if the example included the values of viewModel.selectedDateStateFlow.value, selectedDate, selectedDateToLong and what the date picker shows. Commented Nov 30, 2021 at 10:24
  • Could it be that viewModel.selectedDateStateFlow.value is already "one day less" than what you expect? Did you check its value? Commented Nov 30, 2021 at 10:29
  • when i print value of selectedDate - i get correct date. By Default it current date. timezone on phone GMT+03:00 Moscow Standard Time Commented Nov 30, 2021 at 10:30
  • 1
    Try printing MaterialDatePicker.todayInUtcMilliseconds(). Is that the same value as selectedDateToLong? If not, what is the numerical difference? Also, might as well try ZoneOffset.UTC instead of ZoneId.systemDefault() :-) Commented Nov 30, 2021 at 10:33

1 Answer 1

2

UPD2.

The kind people in the comments were right about a possible error due to using a different time zone. On the advice of one of them, I tried:

val selectedDateToLong = selectedDate.atStartOfDay(ZoneOffset.UTC)...

instead of

val selectedDateToLong = selectedDate.atStartOfDay(ZoneId.systemDefault())...

It helped me

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.