Able to compare dates but unable to get the difference

93 Views Asked by At

I've a date getting fetched from dataBase, and a system date, Here i'm trying to print the number of days of difference.

Here is my DataBean

public class DataBean {
    private Date DAY, EARL_START, LATESTFIN;
    public Date getDAY() {
        return DAY;
    }
    public void setDAY(Date dAY) {
        DAY = dAY;
    }
    public Date getEARL_START() {
        return EARL_START;
    }
    public void setEARL_START(Date eARL_START) {
        EARL_START = eARL_START;
    }
    public Date getLATESTFIN() {
        return LATESTFIN;
    }
    public void setLATESTFIN(Date lATESTFIN) {
        LATESTFIN = lATESTFIN;
    }
}

I'm using the below JSP to compare and display the result.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>

<%@page language="java" import="java.util.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Display Page</title>
</head>
<body>
    <fmt:formatDate value="<%=new java.util.Date()%>" pattern="yyyy-MM-dd"
        var="formattedDate" />
    <table>
        <c:forEach items="${listData}" var="items">
            <fmt:formatDate value="${items.DAY}" pattern="yyyy-MM-dd"
                var="myDate" />
            <tr>
                <td>${myDate}</td>
                <td>${formattedDate}</td>
                <td><c:choose>
                        <c:when test="${myDate < formattedDate}">
                true
                </c:when>
                        <c:otherwise>
false               </c:otherwise>
                    </c:choose></td>
            </tr>
        </c:forEach>

    </table>
</body>
</html>

This is working fine and the result is shown as expected, The result is as below.

2015-04-01 2015-06-22 true  
2015-04-01 2015-06-22 true  
2015-04-01 2015-06-22 true  
2015-04-01 2015-06-22 true  
2015-04-01 2015-06-22 true  
2015-04-01 2015-06-22 true  
2015-04-02 2015-06-22 true  
2015-04-02 2015-06-22 true 

And then I'm trying to get the number of days difference between the two using the below JSP.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>

<%@page language="java" import="java.util.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Display Page</title>
</head>
<body>
    <fmt:formatDate value="<%=new java.util.Date()%>" pattern="yyyy-MM-dd"
        var="formattedDate" />
    <table>
        <c:forEach items="${listData}" var="items">
            <fmt:formatDate value="${items.DAY}" pattern="yyyy-MM-dd"
                var="myDate" />
            <tr>
                <td>${myDate}</td>
                <td>${formattedDate}</td>
                <td><c:choose>
                        <c:when test="${myDate < formattedDate}">
                ${(myDate - formattedDate)}
                </c:when>
                        <c:otherwise>
false               </c:otherwise>
                    </c:choose></td>
            </tr>
        </c:forEach>

    </table>
</body>
</html>

When i run this, it is throwing me the below error

javax.servlet.ServletException: Cannot obtain products from DB
    org.Servlets.RetrieveData.doGet(RetrieveData.java:36)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

org.apache.jasper.JasperException: An exception occurred processing JSP page /DisplayContent.jsp at line 22

19:                 <td>${formattedDate}</td>
20:                 <td><c:choose>
21:                         <c:when test="${myDate < formattedDate}">
22:                 ${(myDate - formattedDate)}
23:                 </c:when>
24:                         <c:otherwise>
25: false               </c:otherwise>


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:403)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:347)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.Servlets.RetrieveData.doGet(RetrieveData.java:33)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

java.lang.NumberFormatException: For input string: "2015-04-01"
    java.lang.NumberFormatException.forInputString(Unknown Source)
    java.lang.Long.parseLong(Unknown Source)
    java.lang.Long.valueOf(Unknown Source)
    org.apache.el.lang.ELArithmetic$LongDelegate.coerce(ELArithmetic.java:212)
    org.apache.el.lang.ELArithmetic.coerce(ELArithmetic.java:400)
    org.apache.el.lang.ELArithmetic.subtract(ELArithmetic.java:314)
    org.apache.el.parser.AstMinus.getValue(AstMinus.java:40)
    org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:187)
    org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:956)
    org.apache.jsp.DisplayContent_jsp._jspx_meth_c_005fwhen_005f0(DisplayContent_jsp.java:262)
    org.apache.jsp.DisplayContent_jsp._jspx_meth_c_005fchoose_005f0(DisplayContent_jsp.java:226)
    org.apache.jsp.DisplayContent_jsp._jspx_meth_c_005fforEach_005f0(DisplayContent_jsp.java:166)
    org.apache.jsp.DisplayContent_jsp._jspService(DisplayContent_jsp.java:116)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:403)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:347)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.Servlets.RetrieveData.doGet(RetrieveData.java:33)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

please let me know where am i going wrong and how can i fix this.

Thanks

0

There are 0 best solutions below