Description
I am 1 day or hours behind from the device time.
Original code
val dateFormatter = LocalDateTime.Format { // yyyy-MM-dd
year()
char('-')
monthNumber()
char('-')
dayOfMonth()
}
val now = Clock.System.now()
val currentDate = now.toLocalDateTime(TimeZone.UTC)
val currentDateInMilli = currentDate.toInstant(TimeZone.UTC).toEpochMilliseconds()
val oldestDateInMilli = now.minus(currentDate.year - 2009L, DateTimeUnit.YEAR, TimeZone.UTC).toEpochMilliseconds()
val previousMonthInMilli = now.minus(1, DateTimeUnit.MONTH, TimeZone.UTC).toEpochMilliseconds()
// Some processing of currentDateInMilli
txtView.text = Instant.fromEpochMilliseconds(it).toLocalDateTime(TimeZone.currentSystemDefault()).format(dateFormatter)
Attempt with
val dateFormatter = LocalDateTime.Format { // yyyy-MM-dd
year()
char('-')
monthNumber()
char('-')
dayOfMonth()
}
val now = Clock.System.now()
val currentDate = now.toLocalDateTime(TimeZone.currentSystemDefault())
val currentDateInMilli = currentDate.toInstant(TimeZone.currentSystemDefault()).toEpochMilliseconds()
val oldestDateInMilli = now.minus(currentDate.year - 2009L, DateTimeUnit.YEAR, TimeZone.currentSystemDefault()).toEpochMilliseconds()
val previousMonthInMilli = now.minus(1, DateTimeUnit.MONTH, TimeZone.currentSystemDefault()).toEpochMilliseconds()
// Some processing of currentDateInMilli
txtView.text = Instant.fromEpochMilliseconds(it).toLocalDateTime(TimeZone.currentSystemDefault()).format(dateFormatter)
Why is it the two sample behaves the same? I thought by passing TimeZone.UTC
and TimeZone.currentSystemDefault())
when converting between Instant
and LocalDate
will set some offset based on provided timezone?
======================================================================================
UPDATE: It seems my actual question here is how to make KotlinX DateTime worked with library like Material DatePicker that requires Java 8 DateTime API?
It turns out that the MaterialDatePicker uses UTC time format, which means it doesn't contain any Time Zone information. We are getting correct behavior back when using Java 8 DateTime but after updating with KotlinX DateTime it is no longer working and hours behind.
I also tried the three approach here and they behave all the same yet on the UI level they behave differently.