Datepicker gives time not between exception

空扰寡人 提交于 2019-12-23 09:10:45

问题


I'm using a datepicker and want to set the min date to today and the max date to today one year ahead.

I do this like:

datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
            cal.add(Calendar.YEAR, 1);
            datePickerDialog.getDatePicker().setMaxDate(cal.getTimeInMillis());

When I don't do - 1000 then I get another exception:

java.lang.IllegalArgumentException: fromDate: Sat Apr 11 23:59:59 CEST 2015 does not precede toDate: Sat Apr 11 08:24:19 CEST 2015

thats because the date may not be equal to today. So I extract 1000 ms.

I don't know how to solve the new exception. I tried to count + 1000 ms on the maxDate but that didn't solve it.

EDIT:

I create my cal like this:

 cal = Calendar.getInstance();
        datePickerDialog = new DatePickerDialog(getActivity(), this, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE));

回答1:


I solved the problem as follows:

            cal.set(Calendar.HOUR_OF_DAY, cal.getMinimum(Calendar.HOUR_OF_DAY));
            cal.set(Calendar.MINUTE, cal.getMinimum(Calendar.MINUTE));
            cal.set(Calendar.SECOND, cal.getMinimum(Calendar.SECOND));
            cal.set(Calendar.MILLISECOND, cal.getMinimum(Calendar.MILLISECOND));
            datePickerDialog.getDatePicker().setMinDate(cal.getTimeInMillis());
            cal.add(Calendar.YEAR, 1);
            cal.set(Calendar.HOUR_OF_DAY, cal.getMaximum(Calendar.HOUR_OF_DAY));
            cal.set(Calendar.MINUTE, cal.getMaximum(Calendar.MINUTE));
            cal.set(Calendar.SECOND, cal.getMaximum(Calendar.SECOND));
            cal.set(Calendar.MILLISECOND, cal.getMaximum(Calendar.MILLISECOND));
            datePickerDialog.getDatePicker().setMaxDate(cal.getTimeInMillis());

I just set the start date to the minimum and the end date to the maximum off that day.




回答2:


Looks like MinDate is higher than MaxDate. As per the exception.




回答3:


Try this:

    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    datePickerDialog.getDatePicker().setMinDate(cal.getTimeInMillis());
    cal.add(Calendar.YEAR, 1);
    datePickerDialog.getDatePicker().setMaxDate(cal.getTimeInMillis());


来源:https://stackoverflow.com/questions/23005411/datepicker-gives-time-not-between-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!