Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

247
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda