Android date.before不起作用

时间:2015-02-09 12:11:57

标签: android android-datepicker

我有一个使用DatePickerFragment的日期选择器。在onDateSet事件中,我使用下面的代码。

 DatePickerDialog.OnDateSetListener ondate = new DatePickerDialog.OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
                          int dayOfMonth) {

        Calendar selected = Calendar.getInstance();
        selected.set(year, monthOfYear, dayOfMonth);

        Calendar now = Calendar.getInstance();
        now.set(now.get(Calendar.YEAR),now.get(Calendar.MONTH),now.get(Calendar.DAY_OF_MONTH));

        if (selected.after(now) || selected.equals(now))
        {
            EditText dateField = (EditText) getActivity().findViewById(Integer.valueOf(textFieldId));
            dateField.setText(new StringBuilder().append(dayOfMonth)
                    .append("-").append(monthOfYear + 1).append("-").append(year)
                    .append(" "));
        }

        else
        {
            Toast.makeText(getActivity(), "this is my Toast message!!! =)",
                    Toast.LENGTH_SHORT).show();
        }


    }

};

此代码不起作用,如果我选择今天的日期,它仍会生成一个Toast消息,而不是将所选值加载到文本字段中。

不确定我做错了什么。

2 个答案:

答案 0 :(得分:0)

你的问题是,在你选择日期的时刻到你得到的时候,现在"日期会在几秒钟内发生,所以两者都有的时间有点不同,现在比选定的晚了。

您可以做的只是比较dayOfMonth,month和year的字段:

    Calendar selected = Calendar.getInstance();
    selected.set(year, monthOfYear, dayOfMonth);

    Calendar now = Calendar.getInstance();


    if(now.get(Calendar.YEAR)== year && now.get(Calendar.MONTH) == month && now.get(Calendar.DAY_OF_MONTH)== day)
    // they are the same date

或者您可以使用简单的日期格式将两者转换为字符串" dd MM yyyy"并将两者作为一个字符串进行比较

答案 1 :(得分:0)

好的感谢GreyBeardedGeek我被放在正确的轨道上,这是我的代码。

 //Set static Date for use in this fragment
Calendar todaysDate = Calendar.getInstance();
final int Year = todaysDate.get(Calendar.YEAR);
final int Month = todaysDate.get(Calendar.MONTH);
final int DayOfMonth = todaysDate.get(Calendar.DAY_OF_MONTH);

/***************** PICKER DIALOGS *****************************/
//DatePicker Dialog
private void showDatePicker(final int fieldId) {
    textFieldId = fieldId;
    DatePickerFragment date = new DatePickerFragment();
    /**
     * Set Up Current Date Into dialog
     */
    Calendar calender = Calendar.getInstance();
    Bundle args = new Bundle();
    args.putInt("year", Year);
    args.putInt("month", Month);
    args.putInt("day", DayOfMonth);
    date.setArguments(args);
    /**
     * Set Call back to capture selected date
     */
   // date.setMinimumDate(calender.get(Calendar.DAY_OF_MONTH),calender.get(Calendar.MONTH),calender.get(Calendar.YEAR));
    date.setCallBack(ondate);
    date.show(getFragmentManager(), "Date Picker");

}


DatePickerDialog.OnDateSetListener ondate = new DatePickerDialog.OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
                          int dayOfMonth) {

       //Calendar selected = Calendar.DATE;
        Date selected =new Date(year, monthOfYear, dayOfMonth);

       Date today = new Date(Year,Month,DayOfMonth);

        if (selected.after(today) || selected.equals(today))// (now.get(Calendar.YEAR)== year && now.get(Calendar.MONTH) == monthOfYear && now.get(Calendar.DAY_OF_MONTH)== dayOfMonth))
        {
            EditText dateField = (EditText) getActivity().findViewById(Integer.valueOf(textFieldId));
            dateField.setText(new StringBuilder().append(dayOfMonth)
                    .append("-").append(monthOfYear + 1).append("-").append(year)
                    .append(" "));
        }

        else
        {
            Toast.makeText(getActivity(), "this is my Toast message!!! =)",
                    Toast.LENGTH_SHORT).show();
        }


    }

};