在Android日期选择器中将SetMax日期设置为30天

时间:2015-11-17 07:43:49

标签: java android date datepicker android-datepicker

我正在使用Datepicker的材质设计库。我需要设置最小和最大日期。最小日期有效但我无法获得距离最小date(current date) 30天的最长日期。谁能知道如何让它在这里工作?

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

2 个答案:

答案 0 :(得分:4)

您可以使用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

答案 1 :(得分:2)

所以这最终奏效了。我所要做的就是调用dpd.setMaxDate(now)而不是dpd.setMaxDate(now.getInstance())

>>>import re
>>>from collections import Counter
>>>d =  re.findall(r'\w+',"['admob.api.oauthcert', 'admob.api.newsession', 'admob.endusercampaign']")
>>>{k:v for k,v in Counter(d).most_common(2)}
>>>[('mob', 3), ('admob', 3), ('api', 2)]