Reputation: 303
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.
Upvotes: 4
Views: 9511
Reputation: 363597
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)
}
Upvotes: 8
Reputation: 29
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));
}
});
Upvotes: 3
Reputation: 1540
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)}"
}
}
Upvotes: 0
Reputation: 847
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 !
Upvotes: 2