在java中获取日期之间的差异(以天为单位)
可能重复:在java中获取日期之间的差异(以天为单位),java,date,Java,Date,可能重复: 我正在尝试这样的事情, 我想从组合框中获取日期 Calendar start = Calendar.getInstance(); Calendar end = Calendar.getInstance(); int Sdate=Integer.parseInt(cmbSdate.getSelectedItem().toString()); int Smonth=cmbSmonth.getSelectedIndex(); int Syear=Integer.parseInt
我正在尝试这样的事情, 我想从组合框中获取日期
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());
我不能减去结束日期和开始日期
如何获得开始日期和结束日期之间的差异???类似
}
是一篇关于Java日期算法的文章。用于此。它比标准的JavaDateTime API好得多。以下是JodaTime中用于计算天数差异的代码:
private static void dateDiff() {
System.out.println("Calculate difference between two dates");
System.out.println("=================================================================");
DateTime startDate = new DateTime(2000, 1, 19, 0, 0, 0, 0);
DateTime endDate = new DateTime();
Days d = Days.daysBetween(startDate, endDate);
int days = d.getDays();
System.out.println(" Difference between " + endDate);
System.out.println(" and " + startDate + " is " + days + " days.");
}
正如orange80所指出的,这在跨越夏令时(或闰秒)时不起作用,在使用一天中的不同时间时,也可能不会给出预期的结果。使用JodaTime可能更容易获得正确的结果,因为我所知道的纯Java 8之前版本的唯一正确方法是使用Calendar的add和before/after方法来检查和调整计算:
start.add(Calendar.DAY_OF_MONTH, (int)diffDays);
while (start.before(end)) {
start.add(Calendar.DAY_OF_MONTH, 1);
diffDays++;
}
while (start.after(end)) {
start.add(Calendar.DAY_OF_MONTH, -1);
diffDays--;
}
你怎么不能呢?例外?错误的日期?我尝试在netbeans中执行此程序n我尝试减去d end.getTime start.getTime()n我收到通知,可以在两个日期对象之间使用-operator。为什么我不能添加我的答案?我没有这样的问题,我有另一个解决方案。可能是因为这个问题被标记为重复。现代评论:我建议你不要使用
Calendar
和SimpleDateFormat
。这些类设计得很糟糕,而且早已过时,后者尤其是出了名的麻烦。Faisal先生,请使用.Thanx中的LocalDate
和ChronoUnit.DAYS
作为答案,但我只需要使用内置函数和apisNow,Java 8与Joda Time的结构基本相同,您可以使用Java.Time.LocalDateTime和ChronoUnit.DAYS.between(startDate,endDate)来实现这一点;你能给我解释一下这段代码吗:1000*60*60*24Date.getTime返回1970年以来的时间,单位为毫秒,所以差值也是毫秒。1000毫秒等于1秒,60秒等于1分钟,60分钟等于1小时,24小时等于1天。当然,你可以将diffTime除以86400000,但是你看不清楚(好的,显然还不清楚;),这个数字是多少。注意,对于跨越夏令时边界的日期范围是不正确的。我也面临同样的问题,但我发现冗长的代码非常无效。虽然这是一个重复的旧版本,但我找到了一个简单的方法来尝试这个公共静态long-getTimeDiff(Date-dateOne,Date-dateTwo){long-diff=0;long-timeDiff=Math.abs(dateOne.getTime()-dateTwo.getTime());diff=TimeUnit.ms.toDays(timeDiff);return diff;}
@abhilash以start.set(2014,日历3月8日)和end.set(2014,日历4月8日)为例;当我运行这个程序时(在德国,3月30日穿越DST,在美国,3月9日穿越DST),我得到了30天的时间差,没有orange80的评论所激发的修复,以及正确的31天。如果在语言环境设置为“印度”的计算机上运行,可能不会有什么区别,因为印度似乎没有夏令时(根据)
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
start.set(2010, 7, 23);
end.set(2010, 8, 26);
Date startDate = start.getTime();
Date endDate = end.getTime();
long startTime = startDate.getTime();
long endTime = endDate.getTime();
long diffTime = endTime - startTime;
long diffDays = diffTime / (1000 * 60 * 60 * 24);
DateFormat dateFormat = DateFormat.getDateInstance();
System.out.println("The difference between "+
dateFormat.format(startDate)+" and "+
dateFormat.format(endDate)+" is "+
diffDays+" days.");
start.add(Calendar.DAY_OF_MONTH, (int)diffDays);
while (start.before(end)) {
start.add(Calendar.DAY_OF_MONTH, 1);
diffDays++;
}
while (start.after(end)) {
start.add(Calendar.DAY_OF_MONTH, -1);
diffDays--;
}