可能重复:
计算两个 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);
}
}
}