我从dataBase获取了一个日期,还有一个系统日期,这里我试图打印差异天数。
这是我的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;
}
}
我使用下面的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}">
true
</c:when>
<c:otherwise>
false </c:otherwise>
</c:choose></td>
</tr>
</c:forEach>
</table>
</body>
</html>
这个工作正常,结果如预期所示,结果如下。
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
然后我尝试使用下面的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>
当我运行此操作时,它会向我发出以下错误
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)
请让我知道我哪里出错了,我该如何解决这个问题。
由于