material-components-android icon indicating copy to clipboard operation
material-components-android copied to clipboard

[DatePicker] Incorrect TimeZones

Open aarontwf opened this issue 6 years ago • 35 comments

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)

image

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

aarontwf avatar Jan 07 '20 21:01 aarontwf

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. Untitled

  • Using v1.2.0-alpha03 - Highlights tomorrow AND draws the black circle around tomorrow....so at least it's consistently incorrect?

thevoiceless avatar Jan 08 '20 03:01 thevoiceless

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()

AdamGrzybkowski avatar Feb 14 '20 10:02 AdamGrzybkowski

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

image

mrea1 avatar May 07 '20 02:05 mrea1

@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?

aarontwf avatar May 08 '20 19:05 aarontwf

@dsn5ft I'm using 1.2.0-beta01 and still shows displaying yesterday as today

b95505017 avatar Jun 01 '20 23:06 b95505017

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.

raajkumars avatar Jun 02 '20 18:06 raajkumars

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)
            }
}

Hesamedin avatar Jun 04 '20 17:06 Hesamedin

@ymarian any update to when this fix be rolled out

prianck avatar Jun 12 '20 15:06 prianck

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.

Manu-Jindal avatar Jun 13 '20 21:06 Manu-Jindal

I'm also facing the same issue from Nigeria. GMT+1 TimeZone.

CodeIsmail avatar Jun 22 '20 20:06 CodeIsmail

https://github.com/material-components/material-components-android/issues/1360#issuecomment-644790081 this solution worked for me.

CodeIsmail avatar Jun 23 '20 07:06 CodeIsmail

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?

paorauk avatar Jul 02 '20 23:07 paorauk

I used the material:1.3.0-alpha02 and it seems it resolves the issue about highlighting yesterday as current day (instead of today).

CsiszerTamas avatar Jul 28 '20 14:07 CsiszerTamas

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?

AbhishekHirapara avatar Aug 10 '20 13:08 AbhishekHirapara

I have the same issue of date range off by one day. Using Material library 1.2.1. Any solution?

IgorGanapolsky avatar Jan 10 '21 23:01 IgorGanapolsky

1.3.0 is currently in beta with the fix.

ikim24 avatar Jan 11 '21 18:01 ikim24

1.3.0 is currently in beta with the fix.

Have you tested the fix?

IgorGanapolsky avatar Jan 11 '21 19:01 IgorGanapolsky

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 avatar Jan 14 '21 01:01 VincentJoshuaET

@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.

ikim24 avatar Jan 14 '21 16:01 ikim24

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());

sabrinanurhidayah avatar Jun 14 '21 19:06 sabrinanurhidayah