1

我是 JSP 的新手,我需要帮助以秒为单位计算 2 个日期之间的差异,它从“yyyy-MM-dd”格式的表单中获取日期输入。但是当我编译它时,我得到了错误。请帮我纠正它。

错误:

java.lang.NullPointerException
    at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1380)
    at java.text.DateFormat.parse(DateFormat.java:355)
    at org.apache.jsp.datediff2_jsp._jspService(datediff2_jsp.java:105)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:722)

我的代码如下:

<%@ page language ="java" import="java.sql.*,java.text.SimpleDateFormat, java.util.Date,java.text.*, java.util.Calendar" %>
<html>
<body bgcolor="pink">
<form method="post">
<input  name="T1" value="2013-07-01"/> 
<input  name="T2" value="2013-07-31"/>
<input type="submit" value="Submit" name="B1">
</form>

<%

String dStart = request.getParameter("T1");
String dStop = request.getParameter("T2");

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

Date d1 = null;
Date d2 = null;

try {
d1 = format.parse(dStart);
d2 = format.parse(dStop);

out.print(d1);

long difference = d2.getTime() - d1.getTime();

long seconds = difference / 1000;

out.print(seconds);

} catch (Exception e) {
e.printStackTrace();
}
%>

</body>
</html>
4

1 回答 1

1

因为,这是一个自我提交的表单,页面第一次加载时T1不会T2设置。因此,如果未设置参数,请使用if块检查s 并跳过日期算术。null那时 JSP 会很好地加载。

<%
  String dStart = request.getParameter("T1");
  String dStop = request.getParameter("T2");

  if (dStart != null && dStop != null) {
    // rest of the code
  }
%>

一旦你提交了日期,你的日期算术代码也会被执行。

于 2013-08-04T19:59:43.313 回答