How to update minDate and maxDate of Datepicker Android

682 Views Asked by At

I have an activity containing 2 DatePickers (From and To) and a Button called "Special request". The first thing i do in my activity is set the minDate and maxDate of both DatePickers. Then by clicking on the Button i wish to update those minDate and maxDate to today's date + one day.

Here's the setDate() method i call first:

public void setDate() {

    final Calendar c = Calendar.getInstance();
    year = c.get(Calendar.YEAR);
    month = c.get(Calendar.MONTH);
    day = c.get(Calendar.DAY_OF_MONTH);
    beginDate.init(year, month, day, null);
    endDate.init(year, month, day, null);
    long minTime = System.currentTimeMillis() - 1000; // -1 second
    beginDate.setMinDate(minTime + 1209600000L); // +14 days
    beginDate.setMaxDate(minTime + 31556952000L); // +1 year
    endDate.setMinDate(minTime + 1209600000L);
    endDate.setMaxDate(minTime + 31556952000L);
}

and the setSpecialDate() wich is very similar:

public void setSpecialDate() {

    long minTime = System.currentTimeMillis() - 1000; // -1 second
    beginDate.setMinDate(minTime + 86400000L); // +1 day
    beginDate.setMaxDate(minTime + 31556952000L); // +1 year
    endDate.setMinDate(minTime + 86400000L);
    endDate.setMaxDate(minTime + 31556952000L);
    final Calendar c = Calendar.getInstance();
    year = c.get(Calendar.YEAR);
    month = c.get(Calendar.MONTH);
    day = c.get(Calendar.DAY_OF_MONTH);
    beginDate.updateDate(year, month, day);
    endDate.updateDate(year, month, day);
}

and my Listener, which is called after setDate() (calling it before doesn't change anything):

public void addRequestListener() {

    specialRequest = (Button) findViewById(R.id.specialRequest);
    specialRequest.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            setSpecialDate();           
        }
    });
}

The problem is that both setMinDate() and setMaxDate() in setSpecialDate() don't work since the DatePickers don't display dates below the ones set in the first place...

0

There are 0 best solutions below