How set maximum date in datepicker dialog in android?

126,316

Solution 1

Use setMaxDate().

For example, replace return new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay) statement with something like this:

    DatePickerDialog dialog = new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay);
    dialog.getDatePicker().setMaxDate(new Date().getTime());
    return dialog;

Solution 2

Get today's date (& time) and apply them as maximum date.

Calendar c = Calendar.getInstance();
c.set(2017, 0, 1);//Year,Mounth -1,Day
your_date_picker.setMaxDate(c.getTimeInMillis());

ALSO WE MAY DO THIS (check this Stackoverflow answer for System.currentTimeMillis() vs Calendar method)

long now = System.currentTimeMillis() - 1000;
dp_time.setMinDate(now);
dp_time.setMaxDate(now+(1000*60*60*24*7)); //After 7 Days from Now

Solution 3

You can try replacing this line:

return new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay);

By those:

DatePickerDialog dpDialog = new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay);
DatePicker datePicker = dpDialog.getDatePicker();

Calendar calendar = Calendar.getInstance();//get the current day
datePicker.setMaxDate(calendar.getTimeInMillis());//set the current day as the max date
return dpDialog;

Solution 4

Try This

I have tried too many solutions but neither them was working,After wasting my half day finally i made a solution.

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

Output:- Disable previous and future calendar

enter image description here

Solution 5

private int pYear;
private int pMonth;
private int pDay;
static final int DATE_DIALOG_ID = 0;

/**inside oncreate */
final Calendar c = Calendar.getInstance();
         pYear= c.get(Calendar.YEAR);
         pMonth = c.get(Calendar.MONTH);
         pDay = c.get(Calendar.DAY_OF_MONTH);


 @Override
     protected Dialog onCreateDialog(int id) {
             switch (id) {
             case DATE_DIALOG_ID:
                     return new DatePickerDialog(this,
                             mDateSetListener,
                             pYear, pMonth-1, pDay);
            }

             return null;
     }
     protected void onPrepareDialog(int id, Dialog dialog) {
             switch (id) {
             case DATE_DIALOG_ID:
                     ((DatePickerDialog) dialog).updateDate(pYear, pMonth-1, pDay);
                     break;
             }
     }   
     private DatePickerDialog.OnDateSetListener mDateSetListener =
         new DatePickerDialog.OnDateSetListener() {
         public void onDateSet(DatePicker view, int year, int monthOfYear,
                         int dayOfMonth) {
                // write your code here to get the selected Date
         }
 };

try this it should work. Thanks

Share:
126,316
Yugesh
Author by

Yugesh

Android Application Developer

Updated on June 09, 2020

Comments

  • Yugesh
    Yugesh about 4 years

    In my application am using date picker to set date.i want to set date picker maximum date is as today date according to system date.i don't know how to set date picker maximum date as today date.Can any one know help me to solve this problem.

    My date picker coding is:

    private int pYear;
    private int pMonth;
    private int pDay;
    static final int DATE_DIALOG_ID = 0;
    
    final Calendar c = Calendar.getInstance();
    pYear = c.get(Calendar.YEAR);
    pMonth = c.get(Calendar.MONTH);
    pDay = c.get(Calendar.DAY_OF_MONTH);
    
    // Date picker
    public Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
    
            DatePickerDialog.OnDateSetListener pDateSetListener = new DatePickerDialog.OnDateSetListener() {
    
                public void onDateSet(DatePicker view, int year,
                        int monthOfYear, int dayOfMonth) {
    
                    pYear = year;
                    pMonth = monthOfYear;
                    pDay = dayOfMonth;
    
                    e_dob.setText(new StringBuilder()
                            // to set date in editext
                            .append(pDay).append("/").append(pMonth + 1)
                            .append("/").append(pYear).append(" "));
                }
            };
            return new DatePickerDialog(this, pDateSetListener, pYear, pMonth,
                    pDay);
    
        }
        return null;
    }
    
  • Yoann Hercouet
    Yoann Hercouet about 11 years
    Your answer deserves an upvote too ;), you show the short way!
  • Saifur
    Saifur over 9 years
    Please add some explanation of what is the code doing.
  • Kishan Vaghela
    Kishan Vaghela over 8 years
    To set max date for all api try this gist.github.com/Kishanjvaghela/7b8738bbb224c5f2e652
  • Anand Savjani
    Anand Savjani over 8 years
    this is perfect and actual answer for set any maximum date
  • superUser
    superUser about 7 years
    I was looking for min date this works as well. Thanks!
  • hasnain_ahmad
    hasnain_ahmad about 7 years
    After setting the setMaxDate of datePicker the setMaxDate is always highlighted and selected, here is the image
  • Nicolas
    Nicolas over 6 years
    @hasnain_ahmad Yes I realized that too, I found no solution yet.
  • Rup
    Rup over 6 years
    Could you add some explanation what you've done here and how it's different to the original code? You also probably don't want the commented out code or error logs.
  • Anurag Pandey
    Anurag Pandey over 6 years
    In this user can set date according to the age criteria needed in his application. Like user can only set the age aboe 18 years then he could use this code .
  • Sriram Nadiminti
    Sriram Nadiminti almost 4 years
    I'm getting error when I paste the code - getActivity() is in red. the error is "can not find symbol method getActivity()". It is suggeting to create a method or create property.
  • Sriram Nadiminti
    Sriram Nadiminti almost 4 years
    Figured it - It is the Activity from where we are calling. If the syntax of DatePickerDialog is shown in comments, it would have been helpful. Upvoting this answer
  • Sunil
    Sunil almost 4 years
    @SriramNadiminti getActivity() is a context you can use "this" if you are using in Activity and for Fragment use "getActivity()"
  • Sriram Nadiminti
    Sriram Nadiminti almost 4 years
    Thank you. How to pass the data to "MyDate", "MyMonth" and "MyYear" which are public Int variables? I have to click twice to get the setText to work
  • J A S K I E R
    J A S K I E R over 3 years
    getInstance is API30. What if I want to use API 25 and below?
  • Bitwise DEVS
    Bitwise DEVS over 2 years
    what is c.set(2017, 0, 1)? Is it the oldest date?