Reputation: 529
I have been working on this for two days solid now. I have gone through numerous posts here but they are not much help to me.
I need to take two dates from a user and calculate the number of days between them. I understand that JODA-time is best and that is not an option ( I am sure the point of this exercise is to go through pain...). I am not worried about leap year or daylight savings time as of yet. I take things in small steps. I have included the code for getting the input but I have not been able to form the syntax correctly to compare the two inputs. Any nudge in the correct direction is appreciated.
<%@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>
Latest for comparing (not pretty):
<%@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>
Upvotes: 2
Views: 7532
Reputation: 529
Here is everything, it works great. Now I want to go make it look good. Thank you for pointing me in the right direction without doing it for me!!!
<%@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");
%>
Upvotes: 1