I am using MaterialDatePicker
in android studio for the user to be able to choose the date of birth, after selecting the date, the returned value is a long
with the timestamp of the selected date (milliseconds since 01/01/1970). The problem is that the result obtained is slightly wrong, example: If the user selects the date 10/05/1998 (1st of October 1998), the date returned is one day before this, that is, the calendar returns 10/04 / 1998 (October 4, 1998).
My code:
Calendar calendar = Calendar.getInstance();
MaterialDatePicker.Builder<Long> builder =
MaterialDatePicker.Builder.datePicker();
MaterialDatePicker<Long> picker = builder.build();
picker.show(getChildFragmentManager(), picker.toString());
picker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener<Long>() {
@Override
public void onPositiveButtonClick(Long selection) {
calendar.setTimeInMillis(selection);
SimpleDateFormat simpleDateFormat = new
SimpleDateFormat("dd/MM//yyyy",Locale.getDefault());
txtBirthDate.setText(simpleDateFormat.format(calendar.getTime()));
}
});
The calendar
instance, now with timeInMillis has the selected date, only a day before. How can I fix this?
【Working solution in 2022】
It turns out that the
MaterialDatePicker
uses UTC time format, which means it doesn't contain any Time Zone information, as the official states here.That means we have to do some conversion work between UTC date-time and the zone date-time, the idea would be using the LocalDateTime api:
1. For showing correct date on picker, we need to convert zone date-time to UTC date-time:
2. For getting zone date-time from picker, we need to convert UTC date-time to zone date-time:
Create Helper Extension Functions:
Show
MaterialDatePicker
inFragment
:Demo: https://youtu.be/9YWr3fgQ214