Android Material Date Picker 日期格式化

huangapple go评论93阅读模式
英文:

Android Material Date Picker Date Format

问题

我在我的应用中使用了Material日期选择器来选择日期。以下是我的代码。

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());
    }
});

当我选择日期时,在设备一上返回的日期格式是dd MMM yyyy,在设备二上是MMM dd, yyyy

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

英文:

I'm using material date picker in my app to select dates. Below is my code.

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());
        }
    });

when I select a date, the date is returned in dd MMM yyyy in device one and MMM dd, yyyy in device two.

I need the date to be returned in same format (Ex: dd MMM yyyy) across all devices. How to achieve this in material date picker.

答案1

得分: 8

The addOnPositiveButtonClickListener listener returns the selected date as a Long value. In this way, you have a long value and not a String.

You can use:

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());
    }
});

In Kotlin:

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)
}
英文:

The addOnPositiveButtonClickListener listener returns the selected date as Long value. In this way you have a long value and not a String.

You can use:

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

In kotlin:

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

答案2

得分: 3

如果您想更改MaterialPicker的日期格式,请使用以下代码,这段代码对我有效:

MaterialDatePicker<Long> materialDatePicker = materialDateBuilder.build();
materialDatePicker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener<Long>() {
    @Override
    public void onPositiveButtonClick(Long selection) {
        // 获取与我们的时区和UTC的偏移量。
        TimeZone timeZoneUTC = TimeZone.getDefault();
        // 它将是负数,所以这是-1
        int offsetFromUTC = timeZoneUTC.getOffset(new Date().getTime()) * -1;
        // 创建日期格式,然后使用我们的偏移量创建日期对象
        SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
        Date date = new Date(selection + offsetFromUTC);

        mEt_last_date_to_apply.setText(simpleFormat.format(date));
    }
});
英文:

If you want to change the MaterialPicker date format then use the below code this code is working for me.

MaterialDatePicker&lt;Long&gt; materialDatePicker=materialDateBuilder.build();
materialDatePicker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener&lt;Long&gt;() {
        @Override
        public void onPositiveButtonClick(Long selection) {
            // Get the offset from our timezone and UTC.
            TimeZone timeZoneUTC = TimeZone.getDefault();
            // It will be negative, so that&#39;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(&quot;yyyy-MM-dd&quot;, Locale.US);
            Date date = new Date(selection + offsetFromUTC);

            mEt_last_date_to_apply.setText(simpleFormat.format(date));
        }
    });

答案3

得分: 2

You should use SimpleDateFormat to format date according to your choice.

@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 string desired format date
    Toast.makeText(MainActivity.this, formatedDate, Toast.LENGTH_LONG).show();
}

Happy Coding!
英文:

You should use SimpleDateFormat to format date according to your choice.

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

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

Happy Coding !

答案4

得分: 0

Here's the translated code part without the comments:

       val builder = MaterialDatePicker.Builder.datePicker()
        builder.setTitleText("选择日期")
        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)}"
            }
        }
英文:

My solution:

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

huangapple
  • 本文由 发表于 2020年8月11日 21:57:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63359732.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定