【发布时间】:2015-08-29 21:06:20
【问题描述】:
我已经为此工作了两天了。我在这里浏览了很多帖子,但它们对我没有多大帮助。
我需要从用户那里获取两个日期并计算它们之间的天数。我知道 JODA 时间是最好的,这不是一个选择(我确信这个练习的目的是经历痛苦......)。到目前为止,我并不担心闰年或夏令时。我采取小步骤。我已经包含了用于获取输入的代码,但我无法正确形成语法来比较两个输入。任何朝着正确方向的轻推都会受到赞赏。
<%@page import="java.util.Calendar" %>
<%@page contentType="text/html" import="java.util.*" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Date Comarision</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<script>
$(function() {
$( ".datepicker" ).datepicker();
});
</script>
</head>
<body>
<form action="Calculate.jsp" method="post">
<p>First Date: <input type="text" name="firstdate" class="datepicker" /></p>
<p>Second Date: <input type="text" name="seconddate" class="datepicker" /></p>
<input type="submit" value="Calculate">
</form>
</body>
</html>
最新比较(不漂亮):
<%@page import="sun.util.calendar.LocalGregorianCalendar.Date"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Calculate</title>
</head>
<body>
<h1>Calculate</h1>
<center>
<!--This is where the issues start. I know I can't be forming this correctly: -->
<%
int Date1= Date.parseInt(request.getParameter("firstdate"));
int Date2= Date.parseInt(request.getParameter("seconddate"));
%>
<BR>
<%
out.println("----- " )+Date1.compareTo(Date2)+("----");
%>
</center>
</body>
</html>
【问题讨论】:
-
一些提示: 1. 使用 Servlet 计算时间差,然后重定向到结果页面 - 不要使用 scriptlet。 2. Java 8 引入了方便的ChronoUnit.between 方法(
DAYS.between(date1, date2))。对于旧版本的 Java,请查看 this answer。 -
谢谢!我要回去工作了!
-
我完成了这个项目!感谢您为我指出正确的方向!它帮助我自己得到它。
-
嗨,路易斯,不客气。如果您认为其他人可以从中受益,请花时间发布您自己的答案并接受它。
标签: java jquery jsp jquery-ui jakarta-ee