添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm using a Material design library for Datepicker . I need to set min and max date. The min date works but I am not able to get the max date which should be 30 days from the min date(current date) . Can anyone know how to make it work here?

public void show() {
    Calendar now = Calendar.getInstance();
    DatePickerDialog dpd = DatePickerDialog.newInstance(
            PostInfo.this,
            now.get(Calendar.YEAR),
            now.get(Calendar.MONTH),
            now.get(Calendar.DAY_OF_MONTH)
    dpd.setMinDate(Calendar.getInstance());
    Calendar calendar = Calendar.getInstance(); 
  calendar.add(Calendar.DAY_OF_MONTH, 30);   
    dpd.setMaxDate(calendar.getInstance());
    dpd.show(getFragmentManager(), "Datepickerdialog");

You can use setMaxDate()

DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), this, year, month, day);
Calendar calendar = Calendar.getInstance();  // this is default system date
                datePickerDialog.getDatePicker().setMinDate(calendar.getTimeInMillis());  //set min date                 // set today's date as min date
                calendar.add(Calendar.DAY_OF_MONTH, 30); // add date to 30 days later
                datePickerDialog.getDatePicker().setMaxDate(calendar.getTimeInMillis()); //set max date
                It says Cannot resolve method getDatePicker(). I think it's because I'm using the material design library?
– spykeburn
                Nov 17, 2015 at 8:04
                I have tried that. It said setmaxDate(java.util.calender) in DatePickerDialog cannot be applied to (java.util.date)
– spykeburn
                Nov 17, 2015 at 8:14
                Same thing - setmaxDate(java.util.calender) in DatePickerDialog cannot be applied to (long)...
– spykeburn
                Nov 17, 2015 at 8:22

So this finally worked. All I had to do is call dpd.setMaxDate(now) instead of dpd.setMaxDate(now.getInstance())

public void show() {
        Calendar now = Calendar.getInstance();
        DatePickerDialog dpd = DatePickerDialog.newInstance(
                PostInfo.this,
                now.get(Calendar.YEAR),
                now.get(Calendar.MONTH),
                now.get(Calendar.DAY_OF_MONTH)
        dpd.setMinDate(Calendar.getInstance());
        now.add(Calendar.DAY_OF_MONTH, 30);
        dpd.setMaxDate(now);
        dpd.show(getFragmentManager(), "Datepickerdialog");
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.