Android DatePickerDialog: Set min and max date for selection

Jyotishgher Astrology
By -
0

 

Android DatePickerDialog: Set min and max date for selection





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

=====================================================================================

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

Post a Comment

0Comments

Post a Comment (0)