Android Material 日期选择器日期格式

Android Material Date Picker Date Format

我在我的应用程序中使用 material 日期选择器选择 select 个日期。下面是我的代码。

MaterialDatePicker.Builder builder = MaterialDatePicker.Builder.datePicker();
    builder.setSelection(Calendar.getInstance().getTimeInMillis());
    builder.setTitleText(getString(R.string.selectDate));
    final MaterialDatePicker materialDatePicker = builder.build();

    btn_pickDate(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            materialDatePicker.show(getSupportFragmentManager(),"DATE_PICKER");
        }
    });
    
    materialDatePicker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener() {
        @Override
        public void onPositiveButtonClick(Object selection) {
            startDate.setText(materialDatePicker.getHeaderText());
        }
    });

当我 select 一个日期时,日期在设备一中的 dd MMM yyyyMMM dd, yyyy 中返回] 在设备二中。

我需要在所有设备上以相同格式返回日期(例如:dd MMM yyyy)。如何在 material 日期选择器中实现这一点。

您应该使用 SimpleDateFormat 根据您的选择格式化日期。

@Override
public void onDateSet(DatePickerDialog view, int Year, int Month, int Day) {

    Calendar selectedDate = Calendar.getInstance().set(Year,Month,Day);
    DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
    String formatedDate = formatter.format(selectedDate );
    //to sting desired format date
    Toast.makeText(MainActivity.this, formatedDate , Toast.LENGTH_LONG).show();
}

编码愉快!

addOnPositiveButtonClickListener 侦听器 returns 将所选日期作为 Long 值。这样你就有了一个 long value 而不是一个 String.

您可以使用:

    MaterialDatePicker<Long> materialDatePicker = builder.build();
    
    materialDatePicker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener<Long>() {
        @Override
        public void onPositiveButtonClick(Long selection) {
           //....
           Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
           calendar.setTimeInMillis(selection);
           SimpleDateFormat format = new SimpleDateFormat("yyyy MM dd");
           String formattedDate  = format.format(calendar.getTime());
        }
    });

在科特林中:

materialDatePicker.addOnPositiveButtonClickListener {
    val utc = Calendar.getInstance(TimeZone.getTimeZone("UTC"))
    utc.timeInMillis = it
    val format = SimpleDateFormat("yyyy-MM-dd")
    val formatted: String = format.format(utc.time)        
}

我的解决方案:

   val builder = MaterialDatePicker.Builder.datePicker()
    builder.setTitleText("Select a Date")
    val picker = builder.build()
    tv_rw_startDate.setOnClickListener {
        picker.show(activity?.supportFragmentManager!!, picker.toString())
        picker.addOnPositiveButtonClickListener {
            val calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"))
            calendar.time = Date(it)
            tv_rw_startDate.text = "${calendar.get(Calendar.DAY_OF_MONTH)}/ " +
                    "${calendar.get(Calendar.MONTH) + 1}/${calendar.get(Calendar.YEAR)}"
        }
    }

如果您想更改 MaterialPicker 日期格式,请使用下面的代码,此代码对我有用。

MaterialDatePicker<Long> materialDatePicker=materialDateBuilder.build();
materialDatePicker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener<Long>() {
        @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));
        }
    });