我现在已经坚持了两天。我在这里经历过很多帖子,但对我来说没什么用。
我需要从用户那里获取两个日期并计算它们之间的天数。我知道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>
答案 0 :(得分:1)
这是一切,它很有效。现在我想让它看起来不错。谢谢你指出我正确的方向,而不是为我做!
<%@page import="java.util.concurrent.TimeUnit"%>
<%@page import="java.util.Calendar" %>
<%@page contentType="text/html" import="java.util.*" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Date Comparison</title>
<%--
Note: Including a claender for the user to selcet desired dates from
--%>
<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>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<script>
$(function() {
$( ".datepicker" ).datepicker();
});
</script>
</head>
<body>
<h1>Select two dates</h1>
<form action="CalculateDate.jsp" method="post">
<p>First Date: <input type="date" name="firstdate" class="datepicker" /></p>
<p>Second Date: <input type="date" name="seconddate" class="datepicker" /></p>
<input type="submit" value="Calculate">
</form>
</body>
</html>
<%@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>
</body>
</html>
<%@page import="java.text.DateFormat"%>
<%@page import="java.util.*"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Calendar" %>
<%
String date1 =request.getParameter("firstdate");
String date2 =request.getParameter("seconddate");
SimpleDateFormat dateformat = new SimpleDateFormat ("E yyyy.MM.dd"); //SDF to display output with day of week
Date displaydate1=new Date(date1); //Turning the inputed date from string
//to date format to be used for the output
Date displaydate2=new Date(date2);
int differenceInDays = (int) ((displaydate2.getTime() - displaydate1.getTime())/(1000*60*60*24));//common method to calculate number of days
out.println("Between " +dateformat.format(displaydate1)+ " and " +dateformat.format(displaydate2)+ " there are " +differenceInDays+ " days");
%>