title: Servlet age calculator gives wrong results
body: <p>I have a problem here, I am trying to calculate the no of months, weeks, days, hours, minutes and seconds of a person based on the person's age. I am using an HTML form to get the data (name and age) and send it to a servlet to calculate the above results. The problem is that the results seem to be accurate when the user provides an age like 29 or less, but when the user enters a large value like 70 or more it gives a wrong negative value in the number of seconds. Here is my servlet code:</p>
  
  <pre><code>package package1;
  
  import java.io.IOException;
  import java.io.PrintWriter;
  
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  public class Servlet_Post_Example extends HttpServlet
  {
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
      {
          response.setContentType("text/html");
          PrintWriter out = response.getWriter();
          String name = "";
          String $age = "";
          try
          {
              name = request.getParameter("name");
              $age = request.getParameter("age");
              int age = Integer.parseInt($age);
              int months = age * 12;
              int weeks = age * 52;
              int days = age * 365;
              int hours = 24 * 365 * age;
              int minutes = 60 * 24 * 365 * age;
              int seconds = 60 * minutes;
              out.println("&lt;html&gt;");
              out.println("   &lt;head&gt;");
              out.println("       &lt;title&gt;Servlet Get Response&lt;/title&gt;");
              out.println("   &lt;/head&gt;");
              out.println("   &lt;body bgcolor=\"#AAFFAA\"&gt;");
              out.println("       &lt;h1&gt;"+name+" you are approximately "+months+" months old, "+weeks+" weeks old, "+days+" days old, "+hours+" hours old, "+minutes+" minutes old and "+seconds+" seconds old!&lt;/h1&gt;");
              out.println("       &lt;a href=\"index.html\"&gt;Click here&lt;/a&gt; to go back to the index page.");
              out.println("   &lt;/body&gt;");
              out.println("&lt;/html&gt;");
          }
          catch(NumberFormatException xcp)
          {
              out.println("&lt;html&gt;");
              out.println("   &lt;head&gt;");
              out.println("       &lt;title&gt;Servlet Get Response&lt;/title&gt;");
              out.println("   &lt;/head&gt;");
              out.println("   &lt;body bgcolor=\"#AAAAAA\"&gt;");
              out.println("       &lt;h1 style=\"color: red\"&gt;There was an error! You must type the a valid integer for your age! You entered "+$age+"&lt;/h1&gt;");
              out.println("       &lt;a href=\"index.html\"&gt;Click here&lt;/a&gt; to go back to the index page.");
              out.println("   &lt;/body&gt;");
              out.println("&lt;/html&gt;");
              //xcp.printStackTrace(out);
          }
          finally
          {
              out.close();
          }
      }
  }
  </code></pre>
  
  <p>The site is hosted <a href="http://www.optdesolver.com/ServletExample1/" rel="nofollow">here.</a> please check it out thanks.</p>