Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I solve this issue faced in datepicker dialog in android?
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/1555262/calculating-the-difference-between-two-java-date-instances">Calculating the difference between two Java date instances</a></p> </blockquote> <p>Please do help me. I'm finding the difference between two dates and everything is fine if the start date and end date are in the same month.. but if I chose today's date as the start date and February 1st as the end date. It shows there difference as 1(one).</p> <p>I was just querying about what went wrong in my own code being a newbie in android. So I cant understand why this question is closed.</p> <pre><code>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&lt;10) { dd_s = "0"+day; } else { dd_s = day+""; } if(mon&lt;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&lt;10) { dd_e = "0"+day; } else { dd_e = day+""; } if(mon&lt;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&gt;0)//(ms2/1000*60*60)&gt;(ms1/1000*60*60))//diff&gt;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&lt;0 )//||(ms2&lt;ms1))//difference&lt;0) { event_days_ed = (EditText)findViewById(R.id.ed_days_event); event_days_ed.setText("Check your end date"); save_btn.setEnabled(false); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload