MaterialDatePicker returns 选择后的日期不正确

MaterialDatePicker returns incorrect date after selection

由于某些未知原因,MaterialDatePicker returns 选择后的日期不正确。 例如,用户位于时区为 America/Tijuana 的墨西哥地区。当他在视觉表示中选择:2021-10-05 时,在结果文本中我有 -1 天,2021-10-04。对于 RU 地区,一切正常。这是代码:

public void startDateSelectionPicker() {
    try {
        MaterialDatePicker<Long> picker = MaterialDatePicker.Builder.datePicker()
                .setSelection(MaterialDatePicker.todayInUtcMilliseconds())
                .setTheme(R.style.CustomDatePickerDialog)
                .build();

        picker.addOnPositiveButtonClickListener(selection -> {
            TimeZone t = TimeZone.getDefault();
            Calendar c1 = Calendar.getInstance();
            c1.setTimeInMillis(selection);
            c1.setTimeZone(TimeZone.getDefault());

//here I need to receive correct date, but receiving -1 from originally selected date.

            String date = ToolsManager.calendarToDate(this, c1, "yyyy-MM-dd");
        });
        picker.show(getSupportFragmentManager(), picker.getTag());
    } catch (IllegalArgumentException e) {
    }
}

public static String calendarToDate(Context context, Calendar calendar, String dateFormat) {
        if (calendar == null) {
            return null;
        }

        Locale locale = context.getResources().getConfiguration().locale;
        DateFormat df = new SimpleDateFormat(dateFormat, locale);

        return df.format(calendar.getTime());
    }

还有当我设置时:.setSelection(MaterialDatePicker.todayInUtcMilliseconds())

日历上显示的是10月18日,但实际上今天是10月20日。

使用:

   c1.setTimeZone(TimeZone.getTimeZone("UTC"));

而不是

   c1.setTimeZone(TimeZone.getDefault());

看看这是否有帮助:

    Calendar c1 = Calendar.getInstance(Locale.getDefault())

我已经用这样的代码解决了这个问题:

public void startDateSelectionPicker() {
        try {
            MaterialDatePicker<Long> picker = MaterialDatePicker.Builder.datePicker()
                    .setSelection(MaterialDatePicker.todayInUtcMilliseconds())
                    .setTheme(R.style.CustomDatePickerDialog)
                    .build();

            picker.addOnPositiveButtonClickListener(selection -> {
                Calendar utc = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
                utc.setTimeInMillis(selection);
                String date = ToolsManager.calendarToDate(this, utc, ToolsManager.LETY_FILTRATION_DATE_FORMAT);
                binding.textview.setText(date);
            });
            picker.show(getSupportFragmentManager(), picker.getTag());
        } catch (IllegalArgumentException e) {
        }
    }

public static String calendarToDate(Context context, Calendar calendar, String dateFormat) {
        if (calendar == null) {
            return null;
        }

        Locale locale = context.getResources().getConfiguration().locale;
        DateFormat df = new SimpleDateFormat(dateFormat, locale);
        TimeZone timeZone = TimeZone.getTimeZone("UTC");
        df.setTimeZone(timeZone);

        Date d = calendar.getTime();
        return df.format(d);
    }

所以,核心求解器是创建带有 UTC 时区的日历(因为它只适用于 UTC 值,并且在格式化时我还必须初始化 UTC 时区,在其他情况下它会根据时间改变值几个小时区域。

这两个链接也有助于理解: https://github.com/material-components/material-components-android/blob/00dc4c6b5af3939418f1c7d1e4c737dc3fb7fd67/docs/components/Picker.md#timezones

https://github.com/material-components/material-components-android/tree/master/catalog/java/io/material/catalog/datepicker

和转换器:https://www.epochconverter.com/