Android DatePickerDialog: Set min and max date for selection

Jyotishgher Astrology
By -
8 minute read
0

 Android DatePickerDialog: Set min and max date for selection

Applications like ticket booking for traveling, theatres, movies, plays, appointments, reminders, etc., require the user to pick a specific day or time for confirming a slot or a booking. In most of these applications, a service is to be picked and then the user has to pick up a slot to further proceed. At this point, while picking up a date, the current calendar month or the nearest available date is generally displayed. However, not all the dates show availability for the slot. Knowingly, a past date is always greyed-out, meaning one cannot book a slot on a past date. 

Android DatePickerDialog: Set min and max date for selection
Moreover, if the service provider has a limited number of slots or no slots for a particular day in the future, those dates are greyed-out. Similarly, the service providers may also take bookings for a range of dates, which they can facilitate accordingly. So in this article, we will show you how you could display a calendar and make a range of dates selectable in it. Follow the below steps once the IDE launches.


CODE SNIPPTS:
private DatePickerDialog.OnDateSetListener mDateSetListener;
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);


DatePickerDialog dialog = new DatePickerDialog(
context,
android.R.style.Theme_Holo_Light_Dialog_MinWidth,
mDateSetListener,
year,month,day);
//Getting Today date to calendar2
final Calendar calendar2 = Calendar.getInstance();
//Set Minimum date of calendar
calendar2.set(2019, 1, 1);
dialog.getDatePicker().setMinDate(calendar2.getTimeInMillis());


dialog.getDatePicker().setMaxDate(new Date().getTime());
Objects.requireNonNull(dialog.getWindow()).setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
CODE EXAMPLE ON EDIT TEXT CLICK: date format like 28-JUL-21

date_dose_first.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);


DatePickerDialog dialog = new DatePickerDialog(
context,
android.R.style.Theme_Holo_Light_Dialog_MinWidth,
mDateSetListener,
year,month,day);
//Getting Today date to calendar2
final Calendar calendar2 = Calendar.getInstance();
//Set Minimum date of calendar
calendar2.set(2019, 1, 1);
dialog.getDatePicker().setMinDate(calendar2.getTimeInMillis());

dialog.getDatePicker().setMaxDate(new Date().getTime());
Objects.requireNonNull(dialog.getWindow()).setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
}
});
mDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {

int day = dayOfMonth;
int yy = year;
int month = ++monthOfYear;
String str_day = day < 10 ? "0" + day : "" + day;
String str_month = month < 10 ? "0" + month : "" + month;
if (str_month.equalsIgnoreCase("01"))
{
str_month="JAN" ;
} else if (str_month.equalsIgnoreCase("02"))
{
str_month="FEB" ;
} else if (str_month.equalsIgnoreCase("03"))
{
str_month="MAR" ;
} else if (str_month.equalsIgnoreCase("04"))
{
str_month="APR" ;
} else if (str_month.equalsIgnoreCase("05"))
{
str_month="MAY" ;
} else if (str_month.equalsIgnoreCase("06"))
{
str_month="JUN" ;
} else if (str_month.equalsIgnoreCase("07"))
{
str_month="JUL" ;
} else if (str_month.equalsIgnoreCase("08"))
{
str_month="AUG" ;
} else if (str_month.equalsIgnoreCase("09"))
{
str_month="SEP" ;
} else if (str_month.equalsIgnoreCase("10"))
{
str_month="OCT" ;
} else if (str_month.equalsIgnoreCase("11"))
{
str_month="NOV" ;
} else if (str_month.equalsIgnoreCase("12"))
{
str_month="DEC" ;
}


date_select = str_day + "-" + str_month+ "-" + String.valueOf(yy).substring(2,4) ;


dateselected.setText(date_select);

Log.d("FirstDoseDate=", "onDateSet: dd/mm/yyy: " +date_select);
/* month = month + 1;
Log.d("FirstDoseDate=", "onDateSet: dd/mm/yyy: " + day + "-" + month + "-" + year+currentYear);

FirstDoseDate = day + "-" + month + "-" + year;*/


//txtdob.setText(date);
}
};

We needed this kind of control in one of our Android application that we were developing because of the application development requirements given by client; so there was need to select the date that is in between user’s date of birth and current date. So we had to extend the DatePickerDialog class.

One of the solution is we can keep a check in the “OnDateSetListener” of the date picker to not let the user choose the dates outside of those date ranges, but that would become somewhat annoying to the user to show alert dialogs of warning and again prompting him to set the date in case the user selects a date outside of the min & max date range set.

=====================================================================================
This code simply show you a DatePickerDialog with Minimum and Maximum date,month and year,whatever you want just modify it.
final Calendar calendar = Calendar.getInstance();
                DatePickerDialog dialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker arg0, int year, int month, int day_of_month) {
                        calendar.set(Calendar.YEAR, year);
                        calendar.set(Calendar.MONTH, (month+1));
                        calendar.set(Calendar.DAY_OF_MONTH, day_of_month);
                        String myFormat = "dd/MM/yyyy";
                        SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.getDefault());
                        your_edittext.setText(sdf.format(calendar.getTime()));
                    }
                },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
                dialog.getDatePicker().setMinDate(calendar.getTimeInMillis());// TODO: used to hide previous date,month and year
                calendar.add(Calendar.YEAR, 0);
                dialog.getDatePicker().setMaxDate(calendar.getTimeInMillis());// TODO: used to hide future date,month and year
                dialog.show();
Tags:

Post a Comment

0Comments

Post a Comment (0)