0

由于某些未知原因,MaterialDatePicker 在选择后返回不正确的日期。例如,用户位于墨西哥地区,时区为: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 日。

4

2 回答 2

2

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

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/

于 2021-10-21T13:22:17.593 回答
1

利用:

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

代替

   c1.setTimeZone(TimeZone.getDefault());

看看这是否有帮助:

    Calendar c1 = Calendar.getInstance(Locale.getDefault())
于 2021-10-20T15:16:11.503 回答