3

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?

4

0 回答 0