可能重复:
如何使用Java计算两个日期之间的差异
我正在尝试类似这样的操作,从组合框中获取日期。
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());
我无法将结束日期减去开始日期。如何获取开始日期和结束日期之间的差异?
Calendar
和SimpleDateFormat
。这些类设计不佳且过时,后者尤其麻烦。相反,请使用LocalDate
和ChronoUnit.DAYS
,它们都来自于Java时间和日期API的现代版本。 - Ole V.V.