How to change Mat-Datepicker date format to DD/MM/YYYY in simplest way?

后端 未结 9 1420
孤城傲影
孤城傲影 2021-02-07 09:00

I\'m setting up a mat-datepicker for DOB and traditionally the display format is MM/DD/YYYY,I need to change it to DD/MM/YYYY with less coding

I tried format tag in mat

9条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-07 09:22

    If you want to change the MaterialDatePicker date format into yyyy-mm-dd then use the below code. This code is working for me.

    MaterialDatePicker materialDatePicker=materialDateBuilder.build();
    materialDatePicker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener() {
        @Override
        public void onPositiveButtonClick(Long selection) {
            // Get the offset from our timezone and UTC.
            TimeZone timeZoneUTC = TimeZone.getDefault();
            // It will be negative, so that's the -1
            int offsetFromUTC = timeZoneUTC.getOffset(new Date().getTime()) * -1;
            // Create a date format, then a date object with our offset
            SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
            Date date = new Date(selection + offsetFromUTC);
    
            mEt_last_date_to_apply.setText(simpleFormat.format(date));
        }
    });
    

    mEt_last_date_to_apply is my EditText

提交回复
热议问题