For some unknown reason, MaterialDatePicker returns incorrect date after selection. For example, user is in Mexico region with timezone: America/Tijuana. When he selects in visual representation: 2021-10-05, in resulting text I have -1 day, 2021-10-04. For RU region everything works fine. Here is the code:
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());
}
And also when I am setting: .setSelection(MaterialDatePicker.todayInUtcMilliseconds())
it display 18 of October on calendar, but really today is 20 of Octobeer.