Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

244
Views
How to Convert LocalDate to Long in Kotlin / Java

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 convertation of the LocalDate into Long and MaterialDatePicker.todayInUtcMilliseconds(). Received different values. What is the mistake of my translation?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!