i have certain codes in JSP and JSTL as following :

<sql:transaction dataSource="${dataBase}">
  <sql:query var="tDate">
       SELECT starttime FROM campaign WHERE cid=25;
  </sql:query>
</sql:transaction>

<c:forEach var="row" items="${tDate.rows}" varStatus="totalRow" step="1">
<c:set var="starttime" value="${row.starttime}" />
<c:set var="datetime" value="${fn:split(starttime,' ')}" />
<c:set var="date" value="${fn:replace(datetime[0],'-',',')}" />
</c:forEach>

<%
    Calendar d = Calendar.getInstance();
    d.set(date);
%>

but apparently, it didn't work. So, how to solve this?

Recommended Answers

All 3 Replies

First thing i observe that you are iterating a collection(row) and getting value date from the collection.
Hence there will not be just one "date", but there will be same nubmer "date" which is equal to the size of the collection "row".

Hence the logic is not correct here, since you are trying to get only one "date" where as there could be many such "date".

Assuming that there is only one "date" in the jstl code ( means you are not iterating any collection).
Using below code can get the value from JSTL to scriptlet.

<%
Calendar d = Calendar.getInstance();
d.set((String)pageContext.getAttribute("date"));
%>

Hope this will help

thanks a lot. It brought me a new idea. I just changed a bit my codes like this:

 <% Calendar c = Calendar.getInstance();
            if(pageContext.getAttribute("date") != null)
            { 
               String datez = (String)pageContext.getAttribute("date"); 
               String dates[] = datez.split(","); 
               String date0 = dates[0]; 
               String date1 = dates[1]; 
               String date2 = dates[2]; 
               int int0 = Integer.parseInt(date0); 
               int int1 = Integer.parseInt(date1) - 1; 
               int int2 = Integer.parseInt(date2); 
               c.set(int0,int1,int2); 
             } 
          %>

@doniandric
If your query is resolved, can you mark this thread as resolved/solved?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.