如何设置上个月的最短日期26

时间:2016-02-20 05:35:18

标签: android

如何设置上个月的最短日期26.这是我的代码。

int difference = 0;
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
    difference = 31;
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
    difference = 30;
} else if (month == 2) {
    if (year % 4 == 0) {
        difference = 29;
    } else
        difference = 28;
}

int minDiffrence = 0;
if (day == 26) {
    minDiffrence = difference;
} else {
    minDiffrence = difference - (26 - day);
}

dpd.getDatePicker().setMinDate(System.currentTimeMillis() - 24 * 60 * 60 * 1000 * minDiffrence);

2 个答案:

答案 0 :(得分:0)

试试这个: -

Calendar calendar = Calendar.getInstance();

                    calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
                    calendar.set(Calendar.DAY_OF_MONTH, 26);

                    dpd.getDatePicker().setMinDate(calendar.getTimeInMillis());

答案 1 :(得分:0)

此代码示例代码涵盖从日期=上个月的开始日期结束日期=当前日期的当前日期。我认为这段代码对您有所帮助。

 datePicker = (DatePicker) findViewById(R.id.datePicker);

    calendar = Calendar.getInstance();
    calendar.add(Calendar.MONTH, -1);
    calendar.set(Calendar.DAY_OF_MONTH, 1);
    datePicker.setMinDate(calendar.getTimeInMillis());
    datePicker.setMaxDate(new Date().getTime());