1

可能重复:
计算两个 Java 日期实例之间的差异

请帮帮我。我发现两个日期之间的差异,如果开始日期和结束日期在同一个月内,一切都很好。但如果我选择今天的日期作为开始日期,选择 2 月 1 日作为结束日期。它显示差异为 1(一)。

我只是在询问作为 android 新手的我自己的代码出了什么问题。所以我不明白为什么这个问题被关闭了。

public void showStartDate(View v) {
        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getFragmentManager(), "startDate");

    }

    public void showEndDate(View v){
        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getFragmentManager(), "endDate");
    }
    public class DatePickerFragment extends DialogFragment
    implements DatePickerDialog.OnDateSetListener /*,OnDateChangedListener*/{

        @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);

     //Toast.makeText(getApplicationContext(),""+ day+"-"+month+1+"-"+year,5).show(); 
    // 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) {
    // Do something with the date chosen by the user
        int mon=month+1;
        Date strtdate,enddate;
        DatePickerDialog d = new DatePickerDialog(getActivity(), this, year, mon, day);

        if(getTag() == "startDate")
        {
            String dd_s = null,mm_s = null;
            Date old_d;
            start_date_dp_btn = (Button)findViewById(R.id.btn_dp_strt_date);
            if(day<10)
            {
                dd_s = "0"+day;
            }
            else
            {
                dd_s = day+"";
            }
            if(mon<10)
            {
                mm_s = "0"+mon;
            }
            else
            {
                mm_s = mon+"";
            }
            start_date_dp_btn.setText(dd_s+"-"+mm_s+"-"+year);
            Toast.makeText(getApplicationContext(),""+ day+"-"+mon+"-"+year,5).show();

            c1 = Calendar.getInstance();

            c1.set(year, mon, day, hr1, min1);
            ms1 = c1.getTimeInMillis(); 

            long date2 = ms1/(1000*60*60*24);
            Log.i("Start date", ""+date2);

        }
        if(getTag() == "endDate")
        {
            String dd_e = null,mm_e = null;
            end_date_dp_btn = (Button)findViewById(R.id.btn_dp_end_date);
            if(day<10)
            {
                dd_e = "0"+day;
            }
            else
            {
                dd_e = day+"";
            }
            if(mon<10)
            {
                mm_e = "0"+mon;
            }
            else
            {
                mm_e = mon+"";
            }
            end_date_dp_btn.setText(dd_e+"-"+mm_e+"-"+year);

            c2 = Calendar.getInstance();
            c2.set(year, mon, day, hr2, min2);
            ms2 = c2.getTimeInMillis();

            long date2 = ms2/(1000*60*60*24);
            Log.i("End date", ""+date2);
            long date1 = ms1/(1000*60*60*24);
            diff = (ms2-ms1)/(1000*60*60*24);

            long difference = date2 - date1;//(strtdate.getTime() - enddate.getTime())/1000*60*60*24;
            Log.i("difference", ""+difference);
            if(difference>0)//(ms2/1000*60*60)>(ms1/1000*60*60))//diff>0)
            {  
               long num = difference+1;//((ms2/1000*60*60)-(ms1/1000*60*60))+1;//((ms2-ms1)/1000*60*60*24)+1;
               event_days_ed = (EditText)findViewById(R.id.ed_days_event);
               event_days_ed.setText((num)+"");
               save_btn.setEnabled(true);
            }
            else if(difference==0)
            {
                event_days_ed = (EditText)findViewById(R.id.ed_days_event);
                event_days_ed.setText((difference+1)+"");
                save_btn.setEnabled(true);
            }
            else if(difference<0 )//||(ms2<ms1))//difference<0)
            {   
                event_days_ed = (EditText)findViewById(R.id.ed_days_event);
                event_days_ed.setText("Check your end date");
                save_btn.setEnabled(false);
            }

        }
  }
4

2 回答 2

1

看到这个&这个

还有一个图书馆Joda来处理日期和时间。有关详细信息,请参阅

于 2013-01-29T07:12:07.130 回答
0

试试这个,

 private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

    public void onDateSet(DatePicker view, int myear, int monthOfYear,
            int dayOfMonth) {
        try {
            selectedDate = new SimpleDateFormat("yyyy/MM/dd").parse(myear
                    + "/" + monthOfYear + "/" + dayOfMonth);
            String.valueOf(year + monthOfYear + dayOfMonth);
            calender();
            currDate = new Date();

            month = monthOfYear;
            day = dayOfMonth;
            year = myear;
            currentdate = currDate.getDay();
            currentmonth = currDate.getMonth();
            currentyear = currDate.getYear();
            String.valueOf(currentyear + currentmonth + currentdate);

            selectedDate.after(currDate);
            selectedDate.before(currDate);

            if ((month >= mMonth && day >= mDay && year >= mYear)
                    || (month == mMonth && day == mDay && year == mYear)) {
                if ((day > mDay) || (day == mDay)) {
                    a = selectedDate.toString();

                    mYear = year;
                    mMonth = monthOfYear;
                    mDay = dayOfMonth;
                    updateDisplay();

                } else if (day < mDay) {

                    Toast.makeText(getApplicationContext(),
                            "Kindly enter valid date", Toast.LENGTH_SHORT)
                            .show();
                    mDateDisplay.setText(currDate.getDate());

                }
            } else if (year < mYear) {
                if (month > mMonth && month == mMonth && month < mMonth) {
                    Toast.makeText(getApplicationContext(),
                            "Kindly enter valid date", Toast.LENGTH_SHORT)
                            .show();
                    mDateDisplay.setText(currDate.getDate());
                }

            } else if (month < mMonth) {
                if (day <= mDay || day >= mDay) {
                    Toast.makeText(getApplicationContext(),
                            "Kindly enter valid date", 100).show();
                    mDateDisplay.setText(currDate.getDate());
                }
            } else if (month > mMonth) {
                if (day <= mDay || day >= mDay) {
                    a = selectedDate.toString();

                    mYear = year;
                    mMonth = monthOfYear;
                    mDay = dayOfMonth;
                    updateDisplay();

                    calender();
                }
            }

            else if (month <= mMonth && day <= mDay && year <= mYear) {
                if (month <= mMonth)
                    Toast.makeText(getApplicationContext(),
                            "Kindly enter valid date", 100).show();
                mDateDisplay.setText(currDate.getDate());

            }

        } catch (Exception e) {
            e.getMessage();

        }
    }
};

protected void calender() {
    // TODO Auto-generated method stub
    c = Calendar.getInstance();
    mYear = c.get(Calendar.YEAR);
    mMonth = c.get(Calendar.MONTH);
    mDay = c.get(Calendar.DAY_OF_MONTH);
    mhour = c.get(Calendar.HOUR);
    mmin = c.get(Calendar.MINUTE);

}
于 2013-01-29T06:49:23.543 回答