如何查找两个日期之间的日期差异?

时间:2015-12-03 11:45:33

标签: java date time calendar

如何在java中查找开始日期和结束日期之间的日期?我试过这段代码,但是在中间我得到了Arrayindexoutofbound异常。

String fromdate="01/01/2015";
String todate="31/12/2015";
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
Date date1 = format.parse(fromdate);
Date date3 = format.parse(todate);
LinkedList list = new LinkedList();        
Date begin = new Date(startdate.getTime());        
list.add(new Date(begin.getTime()));
while (begin.compareTo(enddate) < 0) {
    begin = new Date(begin.getTime() + 86400000);
    list.add(new Date(begin.getTime()));
}
LinkedList hitList=list;
String[] datediff = new String[hitList.size()];
for (int i = 0; i < hitList.size(); i++) {           
    datediff[i] = new SimpleDateFormat("dd/MM/yyyy/EEEE/MMMM").format(hitList.get(i));
}        
return datediff;

输出:

Info:   Fri Apr 17 00:00:00 IST 2015
Info:   Sat Apr 18 00:00:00 IST 2015
Info:   Sun Apr 19 00:00:00 IST 2015
Info:   Mon Apr 20 00:00:00 IST 2015
Warning:   StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exception
java.lang.ArrayIndexOutOfBoundsException: 0
at     org.apache.jsp.holidays.get_005fcalendar_jsp._jspService(get_005fcalendar_jsp.java:141)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at     org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
at java.lang.Thread.run(Thread.java:745)

Info:   Tue Apr 21 00:00:00 IST 2015
Info:   Wed Apr 22 00:00:00 IST 2015
Info:   Thu Apr 23 00:00:00 IST 2015
Info:   Fri Apr 24 00:00:00 IST 2015

2 个答案:

答案 0 :(得分:0)

如果你纠正了这个:

 Date begin = new Date(date1.getTime());        
 list.add(new Date(begin.getTime()));
 while (begin.compareTo(date3) < 0) {

答案 1 :(得分:0)

据我所知,你想要一个包含两个给定日期之间所有日期的数组。这很容易实现:

String fromdate="01/01/2015";
String todate="31/12/2015";
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
Date date1 = format.parse(fromdate);
Date date2 = format.parse(todate);

// getting the number of days between the two dates:
long diffDays = date2.getTime() - date1.getTime();    // difference in ms
diffDays = diffDays/1000/3600/24;                     // difference in days

long baseTime = date1.getTime();
long oneDay = 24 * 3600 * 1000;                       // number of ms in a day
Date[] dateArray = new Date[(int)(diffDays-1)];

for (int i = 1; i < diffDays; i++) {
    // this will create a new date which is i days ahead of the start date
    dateArray[i-1] = new Date(baseTime + oneDay * i);
}

return dateArray;