好,这很奇怪.我有一个非常简单的DatePicker对话框.问题是,无论我选择什么日期,返回的值都恰好是所选日期的一个月.这是我的代码:
活动
btnEventDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
});
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
DateTime dt = new DateTime(year, monthOfYear, dayOfMonth, 0, 0, 0, 0);
Log.d(Constants.TAG, "dt: " + dt.toString());
Log.d(Constants.TAG, "str: " + dateHandler.convertDateToYYYY_MM_DDString(dt));
}
DatePickerFragment
public 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);
return new DatePickerDialog(getActivity(), (OnDateSetListener)getActivity(), year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {}
}
当对话框出现时,它显然设置为今天的日期.假设我选择2015年10月12日,日志输出将显示以下内容:
dt: 2015-09-12T00:00:00.000-06:00
str: 2015-09-12
我肯定错过了什么.有人知道我在做什么错吗?
解决方法:
月份从0到12.从一月到十一月.
尝试将它们与Calendar.“ MONTH”进行比较.
示例:Calendar.OCTOBER == 9
链接:http://developer.android.com/reference/java/util/Calendar.html#MONTH.
注意:如果您使用GregorianCalendar,则可以忽略第13个月.