0

How to find between dates from start date and end date in java? I tried this code but in the middle iam getting Arrayindexoutofbound exception.

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;

output:

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
ParkerHalo
  • 4,341
  • 9
  • 29
  • 51
ram k
  • 19
  • 1

2 Answers2

0

It works if you correct this:

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

As far as i've understood your question you want to have an array containing all dates between the 2 given dates. This is quite easily accomplished by:

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;
ParkerHalo
  • 4,341
  • 9
  • 29
  • 51
  • Its working. but in the middle of print dates, iam getting indexoutofboundexcetion error. I dont know why its throws this error. – ram k Dec 03 '15 at 12:07
  • @ramk which line throws the indexarrayoutofbound? And there's 2 problems with your code: You're using a LinkedList without Parametrization (generics), where does startdate come from? – ParkerHalo Dec 03 '15 at 12:10