Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Minimum date for DatePickerDialog getting IllegalArgumentException Fromdate does not precede toDate

Tags:

java

android

I'm not sure what I am doing wrong? What is fromdate and what is todate? here is my code...

The max date works perfectly, so I'm really confused. It seems to me the new Date().getTime should return the right value or is it something else?

    meetingDateButton.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        DatePickerDialog dpd = new DatePickerDialog(MapActivity.this, new DatePickerDialog.OnDateSetListener() {

                            @Override
                            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                                SimpleDateFormat simpledateformat = new SimpleDateFormat("EEEE");
                                Date date = new Date(year, monthOfYear, dayOfMonth - 1);
                                String dayOfWeek = simpledateformat.format(date);
                                meetingSelectDate.setText(dayOfWeek + " " + (monthOfYear + 1) + "/" + dayOfMonth + "/" + year);
                            }

                        }, mYear, mMonth, mDay);
                        dpd.setTitle("Select Date:");
                        Date maxDate = new Date();
                        maxDate.setTime(new Date().getTime()+(86400000*7));
                        dpd.getDatePicker().setMaxDate(maxDate.getTime());
                        dpd.getDatePicker().setMinDate(new Date().getTime());
                        dpd.show();
                    }
                });
like image 218
Bryanzpope Avatar asked Jul 29 '14 00:07

Bryanzpope


2 Answers

dpd.getDatePicker().setMinDate(new Date().getTime() - 10000);

this is the answer, you have to subtract a little from the time for some reason?

like image 199
Bryanzpope Avatar answered Nov 20 '22 19:11

Bryanzpope


You can get the Calendar instance,and set the time.

long minTimeMillis = System.currentTimeMillis() + 7 * 3600000;
final Calendar minDate = Calendar.getInstance();
minDate.setTimeInMillis(minTimeMillis);
minDate.set(Calendar.HOUR_OF_DAY, minDate.getMinimum(Calendar.HOUR_OF_DAY));
minDate.set(Calendar.MINUTE, minDate.getMinimum(Calendar.MINUTE));
minDate.set(Calendar.SECOND, minDate.getMinimum(Calendar.SECOND));
minDate.set(Calendar.MILLISECOND, minDate.getMinimum(Calendar.MILLISECOND));
dialog.getDatePicker().setMinDate(minDate.getTimeInMillis());
like image 20
MichaelX Avatar answered Nov 20 '22 20:11

MichaelX