英文:
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<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)
}
答案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<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));
}
});
答案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("dd-MM-yyyy");
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("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)}"
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论