Materialdatepicker Get Selected Dates

MaterialDatePicker get selected dates

Just use the addOnPositiveButtonClickListener listener called when the user confirms a valid selection:

For a single date picker:

picker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener<Long>() {
@Override public void onPositiveButtonClick(Long selection) {
// Do something...
//Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
//calendar.setTimeInMillis(selection);

}
});

For a range date picker:

MaterialDatePicker<Pair<Long, Long>> pickerRange = builderRange.build();
pickerRange.show(....);

pickerRange.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener<Pair<Long, Long>>() {
@Override public void onPositiveButtonClick(Pair<Long,Long> selection) {
Long startDate = selection.first;
Long endDate = selection.second;
//Do something...
}
});

How can I get the date from MaterialDatePicker?

To get the date use:

MaterialDatePicker<Long> picker = builder.build();

picker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener<Long>() {
@Override public void onPositiveButtonClick(Long selection) {
// Do something...
}
});

In this way you have the selected date as milliseconds.

If you want to format as a String you can use something like:

Calendar utc = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
utc.setTimeInMillis(selection);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String formatted = format.format(utc.getTime());

MaterialDatePicker returns incorrect date after selection

I have solved this issue with such code:

public void startDateSelectionPicker() {
try {
MaterialDatePicker<Long> picker = MaterialDatePicker.Builder.datePicker()
.setSelection(MaterialDatePicker.todayInUtcMilliseconds())
.setTheme(R.style.CustomDatePickerDialog)
.build();

picker.addOnPositiveButtonClickListener(selection -> {
Calendar utc = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
utc.setTimeInMillis(selection);
String date = ToolsManager.calendarToDate(this, utc, ToolsManager.LETY_FILTRATION_DATE_FORMAT);
binding.textview.setText(date);
});
picker.show(getSupportFragmentManager(), picker.getTag());
} catch (IllegalArgumentException e) {
}
}

public static String calendarToDate(Context context, Calendar calendar, String dateFormat) {
if (calendar == null) {
return null;
}

Locale locale = context.getResources().getConfiguration().locale;
DateFormat df = new SimpleDateFormat(dateFormat, locale);
TimeZone timeZone = TimeZone.getTimeZone("UTC");
df.setTimeZone(timeZone);

Date d = calendar.getTime();
return df.format(d);
}

So, the core solver was to create Calendar with UTC timezone (because it works only with UTC values, and in formatted also I had to init UTC timezone, in other case it was shifting values for some hours depending on time zone.

Also these two links helped to understand:
https://github.com/material-components/material-components-android/blob/00dc4c6b5af3939418f1c7d1e4c737dc3fb7fd67/docs/components/Picker.md#timezones

https://github.com/material-components/material-components-android/tree/master/catalog/java/io/material/catalog/datepicker

And converter: https://www.epochconverter.com/

MaterialDatePicker shows current date instead of needed

You can set the month to which the picker opens with the method constraintsBuilder.setOpenAt().
The default value is the current month if within the bounds otherwise the earliest month within the bounds:

CalendarConstraints.Builder constraintsBuilder = new CalendarConstraints.Builder();

LocalDateTime local = LocalDateTime.of(2020, 11, 1, 0, 0);
long openAt= local.atZone(ZoneId.ofOffset("UTC", ZoneOffset.UTC)).toInstant().toEpochMilli();
//you can also use Calendar.getInstance()...
constraintsBuilder.setOpenAt(openAt);

builder.setCalendarConstraints(constraintsBuilder.build());

You can set a default selection (defaults to no selection) with:

builder.setSelection(....);

Sample Image

How to change the format of Material Date Picker in YYY.MM-DD

The addOnPositiveButtonClickListener listener returns the selected date as Long value.

Don't use the HeaderText.

    start_DatePicker.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());

}
});


Android Material Date Picker Date Format

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

How can I set current date (Today's date) same as selected date in Material Date Picker Android

I could be wrong, but I think you don't need to worry about timezone at all. It's all handled automatically. Remove all code regarding to your Calendar c and just do the selection with

materialDateBuilder.setSelection(Calendar.getInstance().timeInMillis)

Set date and get Date from Material Date Picker in Kotlin Android

MaterialDatePicker accepts CalendarConstraints to open the date picker on a certain month. CalendarConstraints accepts timeInMilliseconds to open calendar on a particular month. MaterialDatePicker has an addOnPositiveButtonClickListener method whose lambda returns the time in milliseconds after the user makes a selection.

You can create MaterialDatePicker like this

val myFormat = "dd/MM/yyyy"
val formattedDate = "01/01/2000"

val sdf = SimpleDateFormat(myFormat)
val date = sdf.parse(formattedDate)
val timeInMillis = date.time

val constraintBuilder = CalendarConstraints.Builder().setOpenAt(
timeInMillis //pass time in milli seconds
).build()

val picker = MaterialDatePicker.Builder.datePicker()
.setTitleText("Select Date")
.setCalendarConstraints(constraintBuilder)
.build()

picker.addOnPositiveButtonClickListener {
val date = Date(it)
val formattedDate = sdf.format(date) // date selected by the user
}

// show picker using this
picker.show(requireActivity().supportFragmentManager, "materialDatePicker")

Instead of using SimpleDateFormatter, you should use LocalDateTime API provided in Java8

Using LocalDateTime API, you can do the same like this

val dateFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy", Locale.getDefault())
val formattedDate = "01/01/2000"
val timeInMillis = LocalDate.parse(formattedDate, dateFormatter)
.atStartOfDay(ZoneId.systemDefault())
.toInstant()
.toEpochMilli()

You can pass this timeInMillis to setOpenAt() method of CalendarConstraintSet.

To get the date after the user makes a selection

val timeInMillis = dateFormatter.format(
// it is the milliseconds received inside lambda of addOnPositiveButtonClickListener
Instant.ofEpochMilli(it)
.atZone(ZoneId.systemDefault()).toLocalDate()
)


Related Topics



Leave a reply



Submit