【问题标题】:android datepicker set max date as 14 years agoandroid datepicker将最大日期设置为14年前
【发布时间】:2015-05-22 16:56:14
【问题描述】:

我正在使用此代码在 android 中生成日期选择器。

DatePickerDialog datePicker = new DatePickerDialog(this, datePickerListener, y, m, d);
datePicker.getDatePicker().setMaxDate(System.currentTimeMillis());

现在max 日期是当前日期。我想把它设置为 14 年前。我该怎么做?

【问题讨论】:

  • 使用System.currentTimeMillis() - (1000 * 60 * 60 * 24 * 365.25 * 14)?
  • 现在最大日期是:2014 年 10 月
  • 抱歉,我忘记了乘数。更新(毫秒 * 秒 * 分钟 * 每日小时 * 年天数 * 年数)。它可能是一个预先计算好的常数:441806400000
  • @DerGolem:谢谢。它现在正在工作。

标签: android datepicker


【解决方案1】:

您可以使用日历来做到这一点

Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.YEAR, -14);
DatePickerDialog datePicker = new DatePickerDialog(this, datePickerListener, y, m, d);
datePicker.getDatePicker().setMaxDate(calendar.getTimeInMillis());

【讨论】:

    【解决方案2】:

    一种方法是使用JodaTime

    datePicker.getDatePicker().setMaxDate(new DateTime().minusYears(14));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多