如何将DatePicker的MaxDate()设置为当前日期之后的一个月? [英] How can I setMaxDate() for DatePicker to one month after the current date?

查看:706
本文介绍了如何将DatePicker的MaxDate()设置为当前日期之后的一个月?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 onCreateDialog

DatePickerDialog dialog = new DatePickerDialog(this, dpickerListener, year_x, month_x, day_x);
        dialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
        dialog.getDatePicker().setMaxDate();

如您所见, setMaxDate()是空的。我想将 datePickerDialog 的最大日期设置为当前日期之后的一个月。
这是我如何获取当前日期:

As you can see, setMaxDate() is empty. I want to set the max date of the datePickerDialog to one month after the current date. Here's how I get the current date:

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);


推荐答案

使用此代码段

Calendar cal=Calendar.getInstance()
cal.add(Calendar.MONTH,1)
long afterTwoMonthsinMilli=cal.getTimeInMillis()
    DatePickerDialog.getDatePicker().setMaxDate(afterTwoMonthsinMilli);

它将当前系统日期读取到日历对象中,并将1添加到指定字段中,在这种情况下 MONTH 并返回日历对象

It reads the current system date into a calendar object and adds 1 to the specified field,In this case MONTH and returns a calendar object

类似地,如果您想向其他字段添加值,请在字段类型为:

Similarly if you want to add values to other fields,Specify the field in the field type as:


  • Calendar.DAY_OF_MONTH

  • Calendar.YEAR

  • Calendar.HOUR

  • Calendar.DAY_OF_MONTH
  • Calendar.YEAR
  • Calendar.HOUR

等。

这篇关于如何将DatePicker的MaxDate()设置为当前日期之后的一个月?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆