3

当我按下按钮以显示 DatePickerDialog 时,对话框显示大一个月。例如,如果我以这样的当前日期开始(使用 joda 库的 DateTime):

   DateTimeZone zone = DateTimeZone.forID("Europe/Athens");

   DateTime dt = new DateTime(zone);


   int year = dt.getYear();
   int month = dt.getMonthOfYear();
   int day = dt.getDayOfMonth();

即 07/08/2014,日期对话框显示比 07/09/2014 大一个月。我不明白为什么会这样。

代表 datePickerFragment 的片段是:

 @SuppressLint("ValidFragment")
    public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{

        @SuppressLint("ValidFragment")
        TextView txtDate;
        GlobalData appState;


         public DatePickerFragment(TextView txtDate) {
            super();
            this.txtDate = txtDate;
        }

        @Override
            public Dialog onCreateDialog(Bundle savedInstanceState) {
                // Use the current date as the default date in the picker
                DateTimeZone zone = DateTimeZone.forID("Europe/Athens");
                DateTime dt = new DateTime(zone);

                int year = dt.getYear();
                int month = dt.getMonthOfYear();
                int day = dt.getDayOfMonth();

                Log.i("DatePickerFragment day month year", day +" "+ month + " "+ year + "");

                // Create a new instance of DatePickerDialog and return it
                return new DatePickerDialog(getActivity(), this, year, month, day);
            }

            public void onDateSet(DatePicker view, int year, int month, int day) {
                appState.setDateUserFrom(year, month, day);

                Log.i("Date day month yerar", "Date changed." +  day+" " + month + " " +year);
                txtDate.setText(new StringBuilder().append(day)
                       .append("-").append(month).append("-").append(year)
                       .append(" "));
            }



    }
4

5 回答 5

12

DatePickerDialog采用[0 for Jan... 11 for Dec],而您monthOfYear的回报. 所以你需要处理月份值。0 to 11DateTime1 to 12-1

用这个:

return new DatePickerDialog(getActivity(), this, year, month - 1, day);
于 2014-08-07T13:12:27.347 回答
2
public Dialog onCreateDialog(Bundle savedInstanceState) {
                // Use the current date as the default date in the picker
                DateTimeZone zone = DateTimeZone.forID("Europe/Athens");
                DateTime dt = new DateTime(zone);

                int year = dt.getYear();
                int month = dt.getMonthOfYear()-1;
                int day = dt.getDayOfMonth();

                Log.i("DatePickerFragment day month year", day +" "+ month + " "+ year + "");

                // Create a new instance of DatePickerDialog and return it
                return new DatePickerDialog(getActivity(), this, year, month, day);
            }

日期选择器中的月份从零开始。因此,您应该从 getMonthOfYear() 中减去一个以将其设置在 datepicker 上。

于 2014-08-07T13:17:13.813 回答
0

我很确定这背后的原因是因为 Android DatePickerDialog 期望基于 0 的月份值。Jodatime 会按照您的预期归还它们(更人性化)。所以只需从月份中减去 1。

澄清一下,大多数日期函数/库默认设计为基于 0 的月份值。例外是明确指出的地方,或者像 Jodatime 这样的第三方库,它们使使用日期的东西变得愉快。

于 2014-08-07T13:13:43.560 回答
0

只是一个猜测。Joda 月从一月的 1 开始,但 java 的日期从 0 开始?因此,如果您将当前日期初始化与 joda 一起使用,日期选择器将显示错误的月份。简单的解决方案:

month = dt.getMonthOfYear() - 1;
于 2014-08-07T13:15:23.793 回答
0

试试这个希望它有效:

DateTimeZone zone = DateTimeZone.forID("Europe/Athens");

       DateTime dt = new DateTime(zone);


       int year = dt.getYear();
       int month = dt.getMonth();
       int day = dt.getDayOfMonth();

或者

DateTimeZone zone = DateTimeZone.forID("Europe/Athens");

   DateTime dt = new DateTime(zone);


   int year = dt.getYear();
   int month = dt.getMonthOfYear() - 1;
   int day = dt.getDayOfMonth();
于 2014-08-07T13:12:34.830 回答