25
21

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

DatePickerDialogで日付の選択範囲を指定

Posted at

DatePickerDialogで日付の選択可能な範囲を指定する方法。
Webにもあまり情報がなかったので、メモ。


// 現在の日付を取得
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);

DatePickerDialog dlgDatePicker = new DatePickerDialog(this, year, month, day);

今回は、2014/1/1 〜 2020/12/31に設定。

// 最大値
// 月は0が1月となる。
GregorianCalendar maxDate = new GregorianCalendar();
maxDate.set(2020, 11, 31);

// 最小値
GregorianCalendar minDate = new GregorianCalendar();
minDate.set(2014, 0, 1);

DatePicker datePicker = dlgDatePicker.getDataPicker();
if (datePicker != null) {
	datePicker.setMaxDate(maxDate.getTimeInMillis());
	datePicker.setMinDate(minDate.getTimeInMillis());
}

設定する値はlong型なっている。
この値は1970年1月1日からの経過時間(単位:ミリ秒)となっているので注意。

25
21
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
25
21

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?