3

ボタンを押して DatePickerDialog を表示すると、ダイアログに 1 か月大きく表示されます。たとえば、次のような現在の日付で開始する場合(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 であり、日付ダイアログには 1 か月大きい 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

0

推測です。Joda の月は 1 月の 1 から始まりますが、Java の日付は 0 ですか? そのため、joda で現在の日付 init を使用すると、日付ピッカーは間違った月を表示します。簡単な解決策:

month = dt.getMonthOfYear() - 1;
于 2014-08-07T13:15:23.793 に答える