Material-components-android: [MaterialDatePicker] Setting a start CalendarConstraint breaks date display

Created on 12 Sep 2019  路  3Comments  路  Source: material-components/material-components-android

Description: If I wish to constrain the datepicker to any time from today to 10 years ago, I used the following code:

val builder = MaterialDatePicker.Builder.datePicker()
val now = ZonedDateTime.now().toInstant().toEpochMilli()
val tenYearsAgo = ZonedDateTime.now().minusYears(10).toInstant().toEpochMilli()

builder.setCalendarConstraints(CalendarConstraints.Builder()
           .setStart(Month.create(tenYearsAgo))
           .setEnd(Month.create(now)).build())

Expected behavior:
While this does prevent a selection from before 2009:
image

It breaks the days in the current month by producing the following:
image

September 2019 starts on Sunday, so my guess is it is pulling September days from 2009 now perhaps.

Android API version: 23

Material Library version: 1.1.0-alpha10

Device: Marshmallow emulator

bug

All 3 comments

We'll take a look at this for the next release. Thanks for the report.

Hi danielwilson1702,

I think the calendar is actually showing August 2019. This is a bug we've fixed since alpha10 related to timezone handling (we now communicate and force timezones in UTC as opposed to using the system default so that device level timezone changes don't affect the calendar).

I re-created your setup using the new APIs and cannot repro.
Thanks for the report. This is fixed in the next release!

builder = MaterialDatePicker.Builder.datePicker();
long now = LocalDateTime.of(2009, 1, 1, 0, 0).atZone(ZoneId.ofOffset("UTC", ZoneOffset.UTC)).toInstant().toEpochMilli();
long tenYearsAgo = now - 315576000000L;
builder.setCalendarConstraints(new CalendarConstraints.Builder()
                .setStart(tenYearsAgo)
                .setEnd(now).build());
MaterialDatePicker<?> picker = builder.build();

Nice work!

Was this page helpful?
0 / 5 - 0 ratings