# Improvement of this method

I'm coding a small web app with no real functionality. It's like a pilot program for something bigger. What I want is construct a program that works as a clock on/off for a restaurant. Actually I'm just trying to simulate one. What I'v done is working but I would like to know whether there is any java class that I can use in a session to calculate the difference between the clock on and off.

Index.jsp

<%@page import="javax.media.jai.operator.EncodeDescriptor"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Project</title>
</head>
<body>
<h1>Employees</h1>
<form action=<%= response.encodeURL("CheckUser") %> method="get">
<input type="hidden" name="userId" value="1">
<input type="submit" value="Diogo">

</form>
</body>
</html>


CheckUser

public class CheckUser extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

String userId = request.getParameter("userId");
User user1 = new User();
user1.setUserId(userId);

HttpSession session = request.getSession();
session.setAttribute("userId", userId);

Hours h1 = (Hours) session.getAttribute("hours");

if (h1 == null){
h1 = new Hours(user1);
}

Calendar cal = new GregorianCalendar();

h1.setMinutes(cal.get(Calendar.MINUTE));

request.setAttribute("hours", h1);
String url = "/show.jsp";

session.setAttribute("hours", h1);

RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);

}


show.jsp

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%@ page import="Business.*, process.*" %>

<%
String userId = (String) session.getAttribute("userId");
Hours h = (Hours) session.getAttribute("hours");
String actualMinute = h.getMinutes();
String calcMinute = h.getCalMinutes();
%>
<h1><%= userId %></h1>
<h1><%= h.getMinutes() %> minuto atual</h1>

<%
if(!actualMinute.equalsIgnoreCase(calcMinute) || calcMinute == "0")
{
%>

<h1><%= h.getCalMinutes() %> minutes</h1>
<%
}else
{
%>
<h1>It is working</h1>
<h1><%= h.getCalMinutes() %></h1>
<%
}
%>

<form action="<%= response.encodeURL("index.jsp") %>" method="get">
<input type="submit" value="return">
</form>
</body>
</html>


## 1 Answer

I assume that Hours is a custom class of yours.

• If you are asking if there is an existing Java class that will automagically calculate the "difference" between two instances of your custom class, the the answer is "No".

• If you are asking (more generally) what is the best class for handling temporal quantities so that you can compare them, calculate differences and so on, then the answer is to look at the JodaTime libraries.

I also note that you are STILL using "Business" as a package name. You should stop it before The Protectors of Java Purity find a way to hack your PC and fill the disc drive with zeros!!!

Seriously, it is bad practice. Don't do it. Follow the standard Java naming conventions, like professional developers do.

• Hi,thanks for the advise. I'm gonna use the Joda library. I can grab the minutes of a day and then do the math. Relating to the name convention, I was following a book. So, should I name packages? Cheers – Camus Dec 20 '11 at 4:31