How to Disable future dates in Android date picker

73,557

Solution 1

Get the DatePicker from DatePickerDialog with getDatePicker(). Set the max date to current date with setMaxDate():

mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());

Requires API level 11.

Solution 2

You can call getDatePicker().setMaxDate(long) on your DatePickerDialog to set today as your maximum date. You can update the function with the same name from the snippet you posted.

Note:: DatePickerDialog is the object that I referenced in the Android Docs from the link I posted.

@Override
protected Dialog onCreateDialog(int id) {
    Calendar c = Calendar.getInstance();
    int cyear = c.get(Calendar.YEAR);
    int cmonth = c.get(Calendar.MONTH);
    int cday = c.get(Calendar.DAY_OF_MONTH);
    switch (id) {
        case DATE_DIALOG_ID:
        //start changes...
        DatePickerDialog dialog = new DatePickerDialog(this, mDateSetListener, cyear, cmonth, cday);
        dialog.getDatePicker().setMaxDate(System.currentTimeMillis());
        return dialog;
        //end changes...
    }
    return null;
}

Try this and give your feedback!!!

Solution 3

Following code help you to disable future dates:

Declare calendar variable globally:

private Calendar myCalendar = Calendar.getInstance();

Put following code in onCreate method:

DatePickerDialog.OnDateSetListener dateListener = new DatePickerDialog.OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
                          int dayOfMonth) {
        // TODO Auto-generated method stub
        myCalendar.set(Calendar.YEAR, year);
        myCalendar.set(Calendar.MONTH, monthOfYear);
        myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        updateLabel();
    }

};

On the button click put the following code:

DatePickerDialog datePickerDialog=new DatePickerDialog(getActivity(), dateListener, myCalendar
                    .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                    myCalendar.get(Calendar.DAY_OF_MONTH));

               //following line to restrict future date selection     
            datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis());
            datePickerDialog.show();

Solution 4

 // Custom Your Future Dates  

 // Example for today and next 3 days :- 3(NUMBER OF NEXT DAYS)*24*60*60*1000l

 // As 24 represents hrs in day

 // As 60 mins 60 secs and convert it to millisec

 //Inside Class which implements DatePickerDialog.OnDateSetListener
 private Calendar mCurrentDate;

 //Inside OnCreate Method
 mDateEditText.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {

   mCurrentDate = Calendar.getInstance();
   int year = mCurrentDate.get(Calendar.YEAR);
   int month = mCurrentDate.get(Calendar.MONTH);
   int day = mCurrentDate.get(Calendar.DAY_OF_MONTH);

   DatePickerDialog mDatePickerDialog = new DatePickerDialog(this, this, year, month, day);
   mDatePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis() + 3 * 24 * 60 * 60 * 1000 l);
  }
 });


 @Override
 public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
  mDateEditText.setText(dayOfMonth + "/" + month + "/" + year);

 }

Solution 5

If user select future date then update datepicker to current date(today)

you can use following code to check selected is future date or not

final Calendar cal = Calendar.getInstance();
datePickerDob.init(currentYear, currentMonth, currentDay,
            new OnDateChangedListener() {

                @Override
                public void onDateChanged(DatePicker view, int year,
                        int monthOfYear, int dayOfMonth) {
                    Calendar selectedCal = Calendar.getInstance();
                    selectedCal.set(year, monthOfYear, dayOfMonth);

                    long selectedMilli = selectedCal.getTimeInMillis();

                    Date datePickerDate = new Date(selectedMilli);
                    if (datePickerDate.after(new Date())) {

                        datePickerDob.updateDate(cal.get(Calendar.YEAR),
                                cal.get(Calendar.MONTH),
                                cal.get(Calendar.DAY_OF_MONTH));

                    } else {


                    }

                }
            });

You can also use compareTo() method

datePickerDate.compareTo(new Date());

Compare the receiver to the specified Date to determine the relative ordering.

Parameters date a Date to compare against.

Returns an int < 0 if this Date is less than the specified Date, 0 if they are equal, and an int > 0 if this Date is greater.

Share:
73,557
venu
Author by

venu

Software Developer

Updated on July 05, 2022

Comments

  • venu
    venu almost 2 years

    How to Disable future dates in Android date picker

    Java Code :

    mExpireDate.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
    
                    // To show current date in the datepicker
                    final Calendar mcurrentDate = Calendar.getInstance();
                    int mYear = mcurrentDate.get(Calendar.YEAR);
                    int mMonth = mcurrentDate.get(Calendar.MONTH);
                    int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);
    
                     DatePickerDialog mDatePicker = new DatePickerDialog(
                            EventRegisterActivity.this, new OnDateSetListener() {
                                public void onDateSet(DatePicker datepicker,
                                        int selectedyear, int selectedmonth,
                                        int selectedday) {
    
                                    mcurrentDate.set(Calendar.YEAR, selectedyear);
                                    mcurrentDate.set(Calendar.MONTH, selectedmonth);
                                    mcurrentDate.set(Calendar.DAY_OF_MONTH,
                                            selectedday);
                                    SimpleDateFormat sdf = new SimpleDateFormat(
                                            getResources().getString(
                                                    R.string.date_card_formate),
                                            Locale.US);
    
                                    mExpireDate.setText(sdf.format(mcurrentDate
                                            .getTime()));
                                }
                            }, mYear, mMonth, mDay);
    
                    mDatePicker.setTitle(getResources().getString(
                            R.string.alert_date_select));
                    mDatePicker.show();
                }
            });
    

    How to do it?

  • Saad Bilal
    Saad Bilal about 10 years
    where to add these lines ondatesetListner ??
  • Dut A.
    Dut A. almost 10 years
    @SaadBilal, add these lines to the onCreateDialog(int id){...} after your dialog id case.
  • Dut A.
    Dut A. almost 10 years
    +1 for this. Instead of .setMaxDate(new Date()), it is better to instead use .setMaxDate(System.currentTimeMillis()) since the expected argument is a long. Otherwise, your approach here is elegant and simple.
  • Ketan Ahir
    Ketan Ahir over 7 years
    There is a bug in v5.0. setMaxDate() will grayed out date but still it can be selected.
  • Manikanta
    Manikanta over 7 years
    This solution may not work in some devices(future days of current month will be displayed). check my answer
  • Steve
    Steve over 7 years
    this answer is working good.disabled future date.But still it is clickable and select that date
  • Narendra Singh
    Narendra Singh over 7 years
    It also disabled current date along with the future dates. I didn't want to disable the current date
  • Rahul Hawge
    Rahul Hawge about 7 years
    @KetanAhir Ya i am also facing the same issue. Were you able to resolve it?
  • Keshav Gera
    Keshav Gera almost 7 years
    mDatePicker.getDatePicker().setMaxDate(System.currentTimeMil‌​lis());
  • Keshav Gera
    Keshav Gera almost 7 years
    mDatePicker.getDatePicker().setMinDate(System.currentTimeMil‌​lis());
  • Aditya Vyas-Lakhan
    Aditya Vyas-Lakhan almost 7 years
    I am using this github.com/wdullaer/MaterialDateTimePicker how can i disable future date?
  • laalto
    laalto almost 7 years
    @AdityaVyas-Lakhan That library's DatePickerDialog has e.g. setMaxDate(Calendar). Post a new question if you need help with that.
  • Amit Verma
    Amit Verma over 6 years
    how to implement this in DialogFragment implements DatePickerDialog.OnDateSetListener
  • Amit Verma
    Amit Verma over 6 years
    Thanks I have implemented this in onCreateDialog // Create a new instance of DatePickerDialog and return it DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), this, year, month, day); datePickerDialog.getDatePicker().setMaxDate(System.currentTi‌​meMillis()); return datePickerDialog;