这是我的onCreateDialog
:
DatePickerDialog dialog = new DatePickerDialog(this, dpickerListener, year_x, month_x, day_x);
dialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
dialog.getDatePicker().setMaxDate();
如您所见,setMaxDate()
为空。我想将datePickerDialog
的最大日期设置为当前日期之后的一个月。
这是我如何获得当前日期:
cal2 = Calendar.getInstance();
currentDay = cal2.get(Calendar.DAY_OF_MONTH);
currentMonth = cal2.get(Calendar.MONTH);
currentYear = cal2.get(Calendar.YEAR);
currentDate = new Date();
currentDate.setDate(currentDay);
currentDate.setMonth(currentMonth);
currentDate.setYear(currentYear);
答案 0 :(得分:2)
使用此代码段
MONTH
它将当前系统日期读入日历对象,并将1添加到指定字段,在本例中为Calendar.DAY_OF_MONTH
并返回日历对象
同样,如果要将值添加到其他字段,请在字段中将字段指定为:
Calendar.YEAR
Calendar.HOUR
int area(int length, int width)
// calculate area of a rectangle;
// pre-conditions: length and width are positive
// post-condition: returns a positive value that is the area
{
if (length<=0 || width <=0)
error("area() pre-condition");
int a = length*width;
if (a<=0)
error("area() post-condition");
return a;
}
等
答案 1 :(得分:2)
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, 1);
dialog.getDatePicker().setMaxDate(calendar.getTimeInMillis);
您可以使用&#34;添加&#34;添加到Calendar.MONTH
答案 2 :(得分:0)
你必须覆盖OnDateChangedListener `new OnDateChangedListener(){
public void onDateChanged(DatePicker view, int year,
int month, int day) {
Calendar newDate = Calendar.getInstance();
newDate.add(Calendar.MONTH, 1);
if (calendar.before(newDate)) {
view.init(minYear, minMonth, minDay, this);
}
}
});`