Skip to content Skip to sidebar Skip to footer

How To Change The Format Of Material Date Picker In Yyy.mm-dd

I want to change the format of Material Date Picker. I am getting it from String its like 13 Apr 2021.But I want to change in 2021-04-13 format. Calendar calendar=Calendar.getI

Solution 1:

The addOnPositiveButtonClickListener listener returns the selected date as Long value. Don't use the HeaderText.

    start_DatePicker.addOnPositiveButtonClickListener(newMaterialPickerOnPositiveButtonClickListener<Long>() {
        @OverridepublicvoidonPositiveButtonClick(Long selection) {
            Calendarcalendar= Calendar.getInstance(TimeZone.getTimeZone("UTC"));
            calendar.setTimeInMillis(selection);
            SimpleDateFormatformat=newSimpleDateFormat("yyyy MM dd");
            StringformattedDate= format.format(calendar.getTime());

        }
    });

    

Solution 2:

Use below code

@OverridepublicvoidonPositiveButtonClick(Object selection) {
SimpleDateFormat dateFormat = newSimpleDateFormat("dd MMM yyyy");

Date varDate=dateFormat.parse(start_DatePicker.getHeaderText());
dateFormat=newSimpleDateFormat("yyyy-MM-dd");
System.out.println("Date :"+dateFormat.format(varDate));}

Post a Comment for "How To Change The Format Of Material Date Picker In Yyy.mm-dd"