DatePicker
次のようなフラグメントを使用します。
private static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// Copy the Date to the EditText set.
dateValue = String.format("%04d", year) + "-" + String.format("%02d", monthOfYear + 1) + "-" + String.format("%02d", dayOfMonth);
}
}
これにより、そもそも日付を取得しやすくなります。日付範囲には次のコードを使用します。
public static List<Date> dateInterval(Date initial, Date final) {
List<Date> dates = new ArrayList<Date>();
Calendar calendar = Calendar.getInstance();
calendar.setTime(initial);
while (calendar.getTime().before(final)) {
Date result = calendar.getTime();
dates.add(result);
calendar.add(Calendar.DATE, 1);
}
return dates;
}
乾杯!
クレジット:これ