gpt4 book ai didi

android - 如何解决 android 中日期选择器对话框中面临的这个问题?

转载 作者:行者123 更新时间:2023-11-30 03:48:21 26 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Calculating the difference between two Java date instances

请帮帮我。我发现两个日期之间的差异,如果开始日期和结束日期在同一个月,一切都很好。但是如果我选择今天的日期作为开始日期,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);
}

}
}

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