Adjusting for time zone with Material3 Datepicker

106 Views Asked by At

I'm trying to create a simple app where the user needs to select a date. My issue is that it defaults to the date according to UTC time unless I add in the zone off set. My confusion is that I thought that val currentZonedDateTime = ZonedDateTime.now(ZoneId.systemDefault()) would account for the time zone. I find my self having to add in the offset. Other wise after 6:00PM it defaults to the next day.

enter image description here

val currentZonedDateTime = ZonedDateTime.now(ZoneId.systemDefault())
    val zoneOffset = currentZonedDateTime.offset
    val datePickerState = rememberDatePickerState(
        initialSelectedDateMillis = currentZonedDateTime.toInstant().toEpochMilli(),
        //initialSelectedDateMillis = currentZonedDateTime.toInstant().toEpochMilli() +    zoneOffset.totalSeconds * 1000,)

If I use the debugger it shows currentZonedDateTime set to America/Chicago. So why does it show UTC time unless I add in the code to add the zoneOffSet? Is there a better way to do this? It works if I add in the commented code. I'm confused why I need to. enter image description here

0

There are 0 best solutions below