1

I want to show the dates between DateA and DateB in a jsp table

For example:

DateA = 23-01-2014 DateB = 26-01-2014

Output:

Dates

  • 23-01-2014
  • 24-01-2014
  • 25-01-2014
  • 26-01-2014

So far I've done this:

index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form name="datepickeer" action="showdates.jsp" method="POST">
<table>
<tr><td>Date début :</td> <td><input type = "date" name = "dateA">
</td><tr>
<tr><td><input type = "submit" name = "submit" value = "submit">
</td></tr>
</table>
</form>
</body>
</html>

showDates.jsp

<%@ page import = "java.util.Date,java.text.SimpleDateFormat,java.text.ParseException"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<% String dateStr = request.getParameter("dateA");
SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
Date result = formater.parse(dateStr);
SimpleDateFormat newFormater = new SimpleDateFormat("dd-MM-yyyy");
out.println(newFormater.format(result));
%>
</body>
</html>

Output when i choose the second of april 2014 is :

02-04-2014

After creating the DateA I should do the same for DateB ,then,what should I do next??

Dimitri Dewaele
  • 10,311
  • 21
  • 80
  • 127
Nihad KH
  • 119
  • 2
  • 4
  • 11

1 Answers1

2

You should use a Calendar and add 1 to Calendar.Day until your date is reached in a loop

    String dateA = "2014-02-02";
    String dateB = "2014-02-06";

    SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
    Date dateStart = null;
    Date dateEnd = null;

    dateStart = formater.parse(dateA);
    dateEnd = formater.parse(dateB);

    Calendar c = GregorianCalendar.getInstance();

    c.setTime(dateStart);

    List<Date> dates = new ArrayList<Date>();

    while (!c.getTime().after(dateEnd)) {
        dates.add(c.getTime());
        c.add(Calendar.DAY_OF_YEAR, 1);
    }

    for (Date element: dates)
    {
        System.out.println("The date is "  + element);

    }
Pat B
  • 1,915
  • 23
  • 40
  • can you give me an example please ? – Nihad KH Apr 10 '14 at 15:36
  • sure.. I will edit my response – Pat B Apr 10 '14 at 17:04
  • thaanks for ur help but your solution didnt work for me it shows this error : HTTP Status 500 - Internal Server Error exception org.apache.jasper.JasperException: java.lang.NullPointerException root cause java.lang.NullPointerException – Nihad KH Apr 11 '14 at 09:20
  • it makes no sense since I'm not referencing Jasper in my code... I tested my code and it works – Pat B Apr 11 '14 at 13:17
  • hi pat , it worked, when i choose for dateA : 2014/04/03 and for dateB: 2014/04/03 the output is : The date is Thu Apr 03 00:00:00 WAT 2014 The date is Fri Apr 04 00:00:00 WAT 2014 The date is Sat Apr 05 00:00:00 WAT 2014 The date is Sun Apr 06 00:00:00 WAT 2014 , I want the input to look like this: 03-04-2014 04-04-2014 05-04-2014 06-04-2014 ,please help me – Nihad KH Apr 22 '14 at 15:26
  • Use a simpleDateFormat... new SimpleDateFormat("dd-MM-yyyy") – Pat B Apr 22 '14 at 16:56