【发布时间】:2023-03-24 21:35:01
【问题描述】:
可能重复:
how to calculate difference between two dates using 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());
我无法减去结束日期和开始日期 如何获取开始日期和结束日期之间的差异??
【问题讨论】:
-
你怎么不行?例外?日期错误?
-
我正在尝试在 netbeans 中执行此程序 n 我试图减去 d end.getTime-start.getTime() n 我被告知可以在两个 Date obj 之间使用 - 运算符。
-
为什么我不能添加我的答案?我没有这样的问题,我有这个问题的另一种解决方案。
-
可能是因为问题被标记为重复。
-
现代评论:我建议你不要使用
Calendar和SimpleDateFormat。这些类设计不佳且早已过时,后者尤其是出了名的麻烦。而是使用来自java.time, the modern Java date and time API 的LocalDate和ChronoUnit.DAYS。