DatePicker片段返回前一个月的日期

DatePicker片段返回前一个月的日期

本文介绍了Android DatePicker片段返回前一个月的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这是奇怪的。我有一个非常简单的 DatePicker 对话框。问题是,无论我选择什么日期,回来的价值就是所选日期前一个月。这是我的代码:



ACTIVITY

  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){
//使用当前日期作为选择器中的默认日期
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);

返回新的DatePickerDialog(getActivity(),(OnDateSetListener)getActivity(),年,月,日);
}

public void onDateSet(DatePicker view,int year,int month,int day){}
}

出现对话框时,显然设置为今天的日期。说我选择2015年10月12日,日志输出显示:

我一定缺少一些东西。任何人都知道我在做错什么?

解决方案

月份从 0 12 。从 January Undecimber



尝试将它们与日历。MONTH



示例: Calendar.OCTOBER == 9



链接:。



注意:如果您使用 GregorianCalendar ,您可以忽略第十三个月。


OK, this is bizarre. I have a DatePicker dialog that is really simple. The problem is that no matter what date I choose, the value that comes back is exactly one month prior to the date selected. Here is my code:

ACTIVITY

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) {}
}

When the dialog appears, it is clearly set to today's date. Say that I select October 12, 2015, the log output shows this:

I must be missing something. Anyone know what I'm doing wrong?

解决方案

Months go from 0 to 12. From January to Undecimber.

Try comparing them against Calendar."MONTH".

Example: Calendar.OCTOBER == 9

Link: http://developer.android.com/reference/java/util/Calendar.html#MONTH.

Note: In case you use GregorianCalendar, you can ignore the thirteenth month.

这篇关于Android DatePicker片段返回前一个月的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 22:35