[DatePicker] Incorrect TimeZones
Description:
There seems to be some strange behavior with time zones with the new date picker I am in New Zealand so our time zone is currently UTC+13 and was testing around 9am
As shown below, it is displaying yesterday as today (I increased the valid date range to include yesterday as it is hidden if not in the range)

Also when setting the initially selected date via MaterialDatePicker.Builder.datePicker().setSelection(), if the millis for a specific UTC date/time is given, it is not accounting for the time zone offset and is selecting the day prior. Currently our workaround is to manually add the millisecond difference from UTC to local time.
Android API version:
Min 27, Target/compile 29
Material Library version:
1.1.0-rc01
Device:
Zebra TC51
Pixel 3a emulator, seeing similar behavior in Denver when passing the current millisecond to setSelection():
-
Using v 1.1.0-rc01 - Highlights tomorrow, although the dialog is correctly drawing the black circle around today.

-
Using v1.2.0-alpha03 - Highlights tomorrow AND draws the black circle around tomorrow....so at least it's consistently incorrect?
I had a similar(I guess) problem with time zone. Once the date was chosen I was displaying it to the user in MM/dd/yyyy format.
I've noticed that on devices with UTC-X timezones it was subtracting one day
I used this code to get the LocalDate
LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), clock.zone()).toLocalDate()
in my case the problem was fixed by hardcoding UTC zoneId instead of clock.zone()
any update here? I am seeing this issue on 1.2.0-alpha06
I am in timezone GMT-5 on May 6th at 9:35pm. The calendar "today" outline shows tomorrow, May 7th

@wcshi Does this also fix:
Also when setting the initially selected date via MaterialDatePicker.Builder.datePicker().setSelection(), if the millis for a specific UTC date/time is given, it is not accounting for the time zone offset and is selecting the day prior. Currently our workaround is to manually add the millisecond difference from UTC to local time.
Or is MaterialDatePicker.Builder.datePicker().setSelection() supposed to be in local time?
@dsn5ft I'm using 1.2.0-beta01 and still shows displaying yesterday as today
I was able to reproduce the issue by setting the test time to December 31, 2019 09:00:00 PM UTC+13:00 (New Zealand) time zone.
I am using 1.3.0-alpha01 and have the same problem.
So, this is my workaround.
val picker = MaterialDatePicker.Builder.datePicker()
.....
.build()
picker.show(supportFragmentManager, picker.toString())
picker.addOnPositiveButtonClickListener {
val utcTime = Date(it)
val format = "yyy/MM/dd HH:mm:ss"
val sdf = SimpleDateFormat(format, Locale.getDefault())
sdf.timeZone = TimeZone.getTimeZone("UTC")
val gmtTime = SimpleDateFormat(format, Locale.getDefault()).parse(sdf.format(utcTime))
gmtTime?.let { date ->
vm.setNextDueDate(date.time)
}
}
@ymarian any update to when this fix be rolled out
Even I am facing this problem, I am in India Timezone (GMT+5:30), and the current date is showing to be yesterdays date. @ymarian @wcshi, any idea when the change will be rolled out? Kindly try to look into this with priority as it is a production app where we are facing this issue.
I'm also facing the same issue from Nigeria. GMT+1 TimeZone.
https://github.com/material-components/material-components-android/issues/1360#issuecomment-644790081 this solution worked for me.
I'm using 1.3.0-alpha01 and still seeing yesterdays date being selected due to the timezone issue. When will the fix be availble in 1.3.0?
I used the material:1.3.0-alpha02 and it seems it resolves the issue about highlighting yesterday as current day (instead of today).
Yes Issue is fixed for highlighting yesterday as current day. but still getting issue with setSelection() method of range selection. Can you please give some solution for this?
I have the same issue of date range off by one day. Using Material library 1.2.1. Any solution?
1.3.0 is currently in beta with the fix.
1.3.0 is currently in beta with the fix.
Have you tested the fix?
1.3.0 is currently in beta with the fix.
Not working for me. I used Instant.now().toEpochMilli() in setSelection() and the previous day gets selected.
@VincentJoshuaET please see the following from https://github.com/material-components/material-components-android/blob/00dc4c6b5af3939418f1c7d1e4c737dc3fb7fd67/docs/components/Picker.md#timezones:
The picker interprets all long values as milliseconds from the UTC Epoch. If you have access to Java 8 libraries, it is strongly recommended you use LocalDateTime and ZonedDateTime; otherwise, you will need to use Calendar.
1.3.0 is currently in beta with the fix.
Not working for me. I used Instant.now().toEpochMilli() in setSelection() and the previous day gets selected.
This is worked for me
public Date dateFromUTC(Date date) {
return new Date(date.getTime() + Calendar.getInstance().getTimeZone().getOffset(new Date().getTime()));
}
// call dateFromUTC in setSelection
MaterialDatePicker.Builder<Long> builder = MaterialDatePicker.Builder.datePicker();
builder.setSelection(dateFromUTC(YOUR_DATE).getTime());