Possible Duplicate:
how to calculate difference between two dates using java
I’m trying something like this,
where I’m trying to get the date from comboboxes
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
int Sdate=Integer.parseInt(cmbSdate.getSelectedItem().toString());
int Smonth=cmbSmonth.getSelectedIndex();
int Syear=Integer.parseInt(cmbSyear.getSelectedItem().toString());
int Edate=Integer.parseInt(cmbEdate.getSelectedItem().toString());
int Emonth=cmbEmonth.getSelectedIndex();
int Eyear=Integer.parseInt(cmbEyear.getSelectedItem().toString());
start.set(Syear,Smonth,Sdate);
end.set(Eyear,Emonth,Edate);
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String startdate=dateFormat.format(start.getTime());
String enddate=dateFormat.format(end.getTime());
I’m not able to subtract the end and start date
How do I get the difference between the start date and end date??
This will not work when crossing daylight savings time (or leap seconds) as orange80 pointed out and might as well not give the expected results when using different times of day. Using JodaTime might be easier for correct results, as the only correct way with plain Java before 8 I know is to use Calendar’s add and before/after methods to check and adjust the calculation: