我试图从Calendar对象获取Year-Month-Day,我知道从index为0开始月份。所以我在月份对象中加了+1。但它仍然是从选择到日期选择器的最后一个月。
我哪里错了?
这就是我的尝试。
这是我的fragmentActivity
SelectionStart
这是我的MainActivity
public class MyDatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar calendar = Calendar.getInstance();
int day = calendar.get(Calendar.DAY_OF_MONTH);
int month = calendar.get(Calendar.MONTH) + 1;
int year = calendar.get(Calendar.YEAR);
DatePickerDialog myDatePicker =
new DatePickerDialog(getActivity(), this, year, month, day);
return myDatePicker;
}
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
EditText et = (EditText) getActivity().findViewById(R.id.editTextDate1);
et.setText(year+"-"+month+"-"+day);
}
}
答案 0 :(得分:5)