Android Date picker dialog setMinDate(long) and setMaxDate(long) issue
My objective was enable date with in certain range & disable the rest for date , so that user can't select those dates. I find these two method for date picker to set the minimum & maximum range.
so first I able to disable all future dates with :
d.getDatePicker().setMaxDate(System.currentTimeMillis());
this was working fine. Than I disable all dates before two weeks ago with this code :
d.getDatePicker().setMaxDate(System.currentTimeMillis() - (2*7*24*60*60*1000));
it was working fine. here 2 for weeks, 7 for days, 24 for hour, 60 for minutes, 60 for seconds & 1000 for millisecond.
so from last two weeks to current date my code was like this
d.getDatePicker().setMaxDate(System.currentTimeMillis() - (2*7*24*60*60*1000));
d.getDatePicker().setMaxDate(System.currentTimeMillis());
The problem was when i desire to set range earlier this month. this process was not working.
As an example i was planning to set date range between 42 weeks to 9 weeks early. the above solution was not. than i found my solution in here https://stackoverflow.com/questions/31049830/how-to-set-the-limit-on-date-in-date-picker-dialog
thanks to Kartheek
solution:
Date today = new Date();
Calendar c = Calendar.getInstance();
c.setTime(today);
c.add( Calendar.WEEK_OF_MONTH, - 42 ); // Subtract 6 weeks
long minDate = c.getTime().getTime(); // Twice!
d.getDatePicker().setMinDate(minDate); //d.getDatePicker().setMaxDate(c.getTimeInMillis());
c.setTime(today);
c.add( Calendar.WEEK_OF_MONTH, - 6 ); // Subtract 6 weeks
long maxDate = c.getTime().getTime(); // Twice!
d.getDatePicker().setMaxDate(maxDate);
so first I able to disable all future dates with :
d.getDatePicker().setMaxDate(System.currentTimeMillis());
this was working fine. Than I disable all dates before two weeks ago with this code :
d.getDatePicker().setMaxDate(System.currentTimeMillis() - (2*7*24*60*60*1000));
it was working fine. here 2 for weeks, 7 for days, 24 for hour, 60 for minutes, 60 for seconds & 1000 for millisecond.
so from last two weeks to current date my code was like this
d.getDatePicker().setMaxDate(System.currentTimeMillis() - (2*7*24*60*60*1000));
d.getDatePicker().setMaxDate(System.currentTimeMillis());
The problem was when i desire to set range earlier this month. this process was not working.
As an example i was planning to set date range between 42 weeks to 9 weeks early. the above solution was not. than i found my solution in here https://stackoverflow.com/questions/31049830/how-to-set-the-limit-on-date-in-date-picker-dialog
thanks to Kartheek
solution:
Date today = new Date();
Calendar c = Calendar.getInstance();
c.setTime(today);
c.add( Calendar.WEEK_OF_MONTH, - 42 ); // Subtract 6 weeks
long minDate = c.getTime().getTime(); // Twice!
d.getDatePicker().setMinDate(minDate); //d.getDatePicker().setMaxDate(c.getTimeInMillis());
c.setTime(today);
c.add( Calendar.WEEK_OF_MONTH, - 6 ); // Subtract 6 weeks
long maxDate = c.getTime().getTime(); // Twice!
d.getDatePicker().setMaxDate(maxDate);
//d.getDatePicker().setMaxDate(c.getTimeInMillis());
Comments
Post a Comment