material-components-android: [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
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 17
- Comments: 33 (9 by maintainers)
Commits related to this issue
- Fixed issue that causes the Datepicker to highlight wrong the day as today. Resolves https://github.com/material-components/material-components-android/issues/882 PiperOrigin-RevId: 310565860 (cherr... — committed to material-components/material-components-android by raajkumars 4 years ago
- [datepicker]Fixed issue that causes the Datepicker to highlight the wrong day as today. Resolves https://github.com/material-components/material-components-android/issues/882 PiperOrigin-RevId: 3153... — committed to material-components/material-components-android by raajkumars 4 years ago
- Fix: 달력 올바른 날짜 선택 안되던 오류 해결 - MaterialDatePicker가 32,400,000 밀리 세컨드 더 빨랐음 - TimeZone 반영이 안되는 것에 대한 이슈가 존재하기 때문 [https://github.com/material-components/material-components-android/issues/882] — committed to linear14/BDProject by linear14 3 years ago
I am using
1.3.0-alpha01and have the same problem.So, this is my workaround.
Feb 2022, This issue is still happening in 1.5.0
@ymarian any update to when this fix be rolled out
@danielandujar If my memory serves right the first implementation of date picker class used local timezone. However in order to avoid a number of bug the implementation was changed later to use UTC. That said you points are valid. It will be inconvenient for developers to change the timezone of the selected date(s) everywhere in the code. Perhaps we could add the following methods to the picker:
Thanks for putting together a demo app. Based on this app and the information you provided, here is what I see:
Current Host Date/time: Fri, Apr 1 2022 10 PM Current Date/time in UTC: Sat, Apr 2, 2022 2:00 AM Selected Date/time in UTC: Sat, Apr 2, 2022 12:00 APM
So yes, the difference would be 2 hours ago. I think that the confusion stems from the fact that date picker receives and returns date values in UTC. Whereas the app is expecting to receive the selected date in local timezone. To convert the selected date in UTC to local date without changing the values you can try something like this:
https://github.com/material-components/material-components-android/issues/1360#issuecomment-644790081 this solution worked for me.
@dsn5ft I’m using
1.2.0-beta01and still shows displaying yesterday as todaySolution for me.
So @raajkumars, this is not what we would expect from the datepicker itself, having to do workarounds for something obvious as dates. The DatePicker should AT BARE MINIMUN say that in the docs and implementation samples. But ideally do Both of these: a) Be defaulted to the device’s default TimeZone b) Have the ability to change the timezone in code.
and by the way, This it is the same reason the ticket was open in the first place
This is worked for me
The secret for me was to set
setTextInputFormat’sSimpleDateFormat’s timezone to be UTC(along with https://github.com/material-components/material-components-android/issues/882#issuecomment-1111374962 )