I'm using the datePicker and I can disable last days of today and later days after 30 days by the following code:
DatePickerDialog datePicker = new DatePickerDialog();
Calendar calender = Calendar.getInstance();
long today = calender.getTimeInMillis();
final long oneDay = 24 * 60 * 60 * 1000L;
Date previousDays = new Date(today - 1000);
datePicker.setMinDate(DateToCalendar(previousDays));
Date nextMonth = new Date(today + 30 * oneDay);
datePicker.setMaxDate(DateToCalendar(nextMonth));
If I want to disable Friday of every month, how can I do this?
the previous dates we need to set the minDate property of the date picker. if we set minDate:0 then it will disable all the previous dates. and we set input attribute min:current_date then it will disable all the previous dates.
In the Flutter date range picker, you can enable or disable the past dates by using the enablePastDates property of the calendar. When setting the enablePastDates property value as false, you can't able to select the past dates.
You can use this library Material Date Time Picker, here you can set an option to show specific dates, For Example:
datePicker.setSelectableDays(Calendar[] days)
And pass array of Calendar
as an parameter which contains all the selectable date.
Use custom DatePickerDialog
add this in build.gradle
compile 'com.wdullaer:materialdatetimepicker:3.5.1'
implement
DatePickerDialog.OnDateSetListener to your class
and import
import com.wdullaer.materialdatetimepicker.date.DatePickerDialog; instead of 'import android.app.DatePickerDialog;'
and add this method
private void showDatePicker() {
Calendar calendar = Calendar.getInstance();
DatePickerDialog dpd = DatePickerDialog.newInstance(
this,
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH)
);
dpd.show(getActivity().getFragmentManager(), "DatePickerDialog");
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
String a = "07-03-2018"; // example
java.util.Date date = null;
try {
date = sdf.parse(a);
} catch (ParseException e) {
e.printStackTrace();
}
calendar = dateToCalendar(date);
System.out.println(calendar.getTime());
List<Calendar> dates = new ArrayList<>();
dates.add(calendar);
Calendar[] disabledDays1 = dates.toArray(new Calendar[dates.size()]);
dpd.setDisabledDays(disabledDays1);
}
private Calendar dateToCalendar(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar;
}
@Override
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
String date = dayOfMonth+"/"+(monthOfYear+1)+"/"+year;
textview.setText(date);
}
call showDatePicker().
for multiple days
Change this
String[] holidays = {"07-03-2018","05-03-2018","10-03-2018"};
java.util.Date date = null;
for (int i = 0;i < holidays.length; i++) {
try {
date = sdf.parse(holidays[i]);
} catch (ParseException e) {
e.printStackTrace();
}
calendar = dateToCalendar(date);
System.out.println(calendar.getTime());
List<Calendar> dates = new ArrayList<>();
dates.add(calendar);
Calendar[] disabledDays1 = dates.toArray(new Calendar[dates.size()]);
dpd.setDisabledDays(disabledDays1);
}
That is not possible with the android datepicker and you need to create a custom picker for yourself. See MaterialDateTimePicker
To disable Sundays you have to pass the array like this
GregorianCalendar g1=new GregorianCalendar();
g1.add(Calendar.DATE, 1);
GregorianCalendar gc = new GregorianCalendar();
gc.add(Calendar.DAY_OF_MONTH, 30);
List<Calendar> dayslist= new LinkedList<Calendar>();
Calendar[] daysArray;
Calendar cAux = Calendar.getInstance();
while ( cAux.getTimeInMillis() <= gc.getTimeInMillis()) {
if (cAux.get(Calendar.DAY_OF_WEEK) != 1) {
Calendar c = Calendar.getInstance();
c.setTimeInMillis(cAux.getTimeInMillis());
dayslist.add(c);
}
cAux.setTimeInMillis(cAux.getTimeInMillis() + (24*60*60*1000));
}
daysArray = new Calendar[dayslist.size()];
for (int i = 0; i<daysArray.length;i++)
{
daysArray[i]=dayslist.get(i);
}
datePickerDialog.setSelectableDays(daysArray);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With