gpt4 book ai didi

java - JSP 用户选择日期之间的天数

转载 作者:行者123 更新时间:2023-11-30 10:58:21 24 4
gpt4 key购买 nike

我已经为此工作了两天了。我在这里浏览了很多帖子,但它们对我帮助不大。

我需要从用户那里获取两个日期并计算它们之间的天数。我知道 JODA-time 是最好的,这不是一个选择(我确信这个练习的目的是经历痛苦......)。到目前为止,我并不担心闰年或夏令时。我采取小步骤。我已经包含了获取输入的代码,但是我无法正确地形成语法来比较两个输入。任何朝着正确方向的插入都是值得赞赏的。

<%@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>

最佳答案

这就是一切,效果很好。现在我想让它看起来不错。感谢您在没有为我做的情况下为我指明了正确的方向!!!

<%@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");

%>

关于java - JSP 用户选择日期之间的天数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32290659/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com