Cannot get the right date format in MaterialDatePicker android


I tried below code but i can not get the result which I want.

    MaterialDatePicker.Builder<Pair<Long, Long>> builder = MaterialDatePicker.Builder.dateRangePicker();
    builder.setTitleText("Select A Date");
    final MaterialDatePicker<Pair<Long, Long>> materialDatePicker =;
    date_picker.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
    materialDatePicker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener<Pair<Long, Long>>() {
        public void onPositiveButtonClick(Pair<Long, Long> selection) { 
            Long start = selection.first;
            Calendar start_cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
            SimpleDateFormat simpleDateFormat_start = new SimpleDateFormat("YYYY-MM-DD");
            start_date = simpleDateFormat_start.format(start_cal.getTime());
            Long end = selection.second;
            Calendar end_cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
            SimpleDateFormat simpleDateFormat_end = new SimpleDateFormat("YYYY-MM-DD");
            end_date = simpleDateFormat_end.format(end_cal.getTime());
            String final_text = start_date + "  to  " + end_date;
            Log.e("Check65", "Date : " + final_text);

I want
startdate: 2021-01-01
enddate: 2021-02-02

but i get
startdate: 2021-01-01
enddate: 2021-02-32


In your SimpleDateFormat you should use YYYY-MM-dd instead of YYYY-MM-DD as uppercase DD means day of the year and you want the day of the month. For reference

Answered By – Stoyan Milev

Answer Checked By – Dawn Plyler (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.