gpt4 book ai didi

android - DatePickeDialog 在 android 中显示多一个月

转载 作者:行者123 更新时间:2023-11-29 14:21:40 25 4
gpt4 key购买 nike

当我按下按钮以显示 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 的 fragment 是:

 @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(" "));
}



}

最佳答案

DatePickerDialog采用 monthOfYear0 到 11 [1 月为 0...Dec 为 11],并且您的 DateTime 返回 1 到 12 。所以你需要用月值做 -1

使用这个:

return new DatePickerDialog(getActivity(), this, year, month - 1, day);

关于android - DatePickeDialog 在 android 中显示多一个月,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25183400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com