title: Java SimpleDateFormat Not working properly
body: <p>This is my first post, and is about a concern that carry me on over more than a week without an useful answer. I have a datepicker on a form(JSP) with hibernate and spring, here is the "form" tag:</p>
  
  <pre><code>&lt;%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%&gt;
  &lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%&gt;
  &lt;%@ taglib prefix="tag" uri="/WEB-INF/taglibs/customTaglib.tld"%&gt;
  Private Code with Head, Meta, and opening tags
  &lt;form:form method="POST" action="${pageContext.request.contextPath}/save.htm"
                              modelAttribute="formName"&gt;
                              &lt;div class="row"&gt;
                                  &lt;!-- /.col-lg-4 (nested) --&gt;
                                  &lt;div class="col-lg-4 col-sm-4 col-xs-12"&gt;
  
                                      &lt;div class="form-group"&gt;
                                          &lt;label&gt;Starting Date: &lt;/label&gt;
                                          &lt;form:input type="text" class="form-control"
                                              id="agregar_fechainicio" path="agregar_fechainicio" maxlength="10" onkeypress="return event.charCode &gt;= 48 &amp;&amp; event.charCode &lt;=57" /&gt;
                                      &lt;/div&gt;
                                      &lt;div class="form-group"&gt;
                                          &lt;label&gt;Ending Date: &lt;/label&gt;
                                          &lt;form:input type="text" class="form-control"
                                              id="agregar_fechatermino" path="agregar_fechatermino" maxlength="10" onkeypress="return event.charCode &gt;= 48 &amp;&amp; event.charCode &lt;=57" /&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
                                  &lt;!-- /.col-lg-4 (nested) --&gt;
                              &lt;/div&gt;
  
                              &lt;!-- /.row (nested) --&gt;
                              &lt;div class="row"&gt;
                                  &lt;div class="col-lg-4 col-sm-offset-8"&gt;
                                      &lt;input type="submit" class="btn btn-default"
                                          value="Save" /&gt; &lt;input type="reset" class="btn"
                                          value="Reset" /&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/form:form&gt;
                          &lt;!-- /.row --&gt;
                      &lt;/div&gt;
                      &lt;!-- /.panel-body --&gt;
                  &lt;/div&gt;
                  &lt;!-- /.panel --&gt;
              &lt;/div&gt;
  
              &lt;!-- /.col-lg-12 --&gt;
  
  
          &lt;/div&gt;
          &lt;!-- /.row --&gt;
  
      &lt;/div&gt;
      &lt;!-- /#page-wrapper --&gt;
  
  &lt;/div&gt;
  &lt;!-- /#wrapper --&gt;
  
  Private Code with others scripts
  &lt;script
      src="${pageContext.request.contextPath}/resources/js/jquery-ui.js"&gt;&lt;/script&gt;
  &lt;link rel="stylesheet"
      href="${pageContext.request.contextPath}/resources/cs/jquery-ui.css"&gt;
  &lt;/script&gt;
  &lt;script&gt;
      $(function() {
          $.datepicker.setDefaults($.datepicker.regional["es"]);
          $("#agregar_fechainicio").datepicker({
              buttonImage : '${contextPath}/resources/images/calendar2.png',
              buttonImageOnly : true,
              showButtonPanel : false,
              minDate : "0D",
              defaultDate : "+1d",
              changeMonth : true,
              changeYear : true,
              numberOfMonths : 1,
              fixFocusIE : false,
              onSelect : function(selectedDate) {
                  $("#to").datepicker("option", "minDate", selectedDate);
              }
          });
      });
      $(function() {
          $.datepicker.setDefaults($.datepicker.regional["es"]);
          $("#agregar_fechatermino").datepicker({
              buttonImage : '${contextPath}/resources/images/calendar2.png',
              buttonImageOnly : true,
              showButtonPanel : false,
              minDate : "1D",
              defaultDate : "+1d",
              changeMonth : true,
              changeYear : true,
              numberOfMonths : 1,
              fixFocusIE : false,
              onSelect : function(selectedDate) {
                  $("#to").datepicker("option", "minDate", selectedDate);
              }
          });
      });
  &lt;/script&gt;
  </code></pre>
  
  <p></p>
  
  <p></p>
  
  <p>Now, The controller Has the method:</p>
  
  <pre><code>@RequestMapping(value = "/save.htm", method = RequestMethod.POST)
  public ModelAndView saveController(
          @ModelAttribute("formName") FormName formName,
          Integer offset, BindingResult bindingResult, ModelMap model,
          Integer maxResults) {
      try {
          int var = 0;
          model.addAttribute("offset", offset);
          model.addAttribute("maxResults", maxResults);
              // Bloque save
              int retorno = service.add(formName);
                      *PRIVATE CODE NOT RELATIONED WITH METHOD*
                  field = message.cargaSucess();
                  msg = alerts.sucessForms(field);
                  model.addAttribute("error", "");
                  model.addAttribute("warning", "");
                  // send message over success
                  model.addAttribute("success", msg);
                  formName = new FormName();
  
              model.remove(formName);
              model.addAttribute("formName",
                      new FormName());
      } catch (Exception e) {
          logger.error("Error in method saveController &lt;["
                  + e.getMessage() + "]&gt;");
          field = mensajes.cargaPagError();
          msg = alerts.errorCargaForms(field);
          model.addAttribute("error", msg);
          model.addAttribute("warning", "");
          model.addAttribute("success", "");
          return new ModelAndView("home/dates", "model",
                  model);
      }
      return new ModelAndView("home/dates", "model", model);
  }
  </code></pre>
  
  <p>And I have the following things in other classes(To check if date is correct):</p>
  
  <pre><code>public String validation(
          FormNew formNew)
          throws ParseException {
      String fields = "";
      DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
  
      if (format.parse(formNew.getAgregar_fechainicio())
              .compareTo(
                      format.parse(formNew
                              .getAgregar_fechatermino())) &gt;= 1) {
          fields = fields
                  .concat("Ending date cant be newer than starting date. &lt;br&gt;");
      }
      if (format.parse(formNew.getAgregar_fechainicio())
              .compareTo(
                      format.parse(formNew
                              .getAgregar_fechatermino())) == 0) {
          fields = fields
                  .concat("Starting and Edning date are same. &lt;br&gt;");
      }
  }
  </code></pre>
  
  <p>(There are some code that is not showed by copyright)</p>
  
  <p>Service Class:</p>
  
  <pre><code>@Autowired
  TABLE_DATEDAO tABLE_DATEDAO;
  
  @Override
  public int add(NewForm newForm) {
      TABLE_DATE tABLE_DATE = new TABLE_DATE();
      // System.out.println("FORMATO FECHA +newForm.getAgregar_fechainicio());
      try {
  
              DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
              tABLE_DATE.setFechainicio(dateFormat
                      .parse(newForm
                              .getAgregar_fechainicio())); // *
              tABLE_DATE.setFechatermino(dateFormat
                      .parse(newForm
                              .getAgregar_fechatermino())); // *
              total = dateFormat.parse(
                      newForm.getAgregar_fechainicio())
                      .compareTo(
                              dateFormat.parse(newForm
                                      .getAgregar_fechatermino()));
              Date date = new Date();
              tABLE_DATE.setFechacreacion(dateFormat.parse(dateFormat
                      .format(date)));
              // System.out.println("FORMATO FECHA "+dateFormat.parse(dateFormat.format(date)));
  
  
      } catch (Exception e) {
           e.printStackTrace();
           System.out.println("Wrong at date service method: "+e.getMessage());
      }
  
      return tABLE_DATEDAO.add(tABLE_DATE);
  }
  </code></pre>
  
  <p>and a last method:</p>
  
  <pre><code>public int comparaFecha(String fechaInit, String fechaFin) {
      int salida = -2;
      try {
          logger.info("FECHA INICIO: "+fechaInit);
          String pattern = "dd-MM-yyyy";
          SimpleDateFormat format = new SimpleDateFormat(pattern);
          logger.info("salida = dat2.compareTo(dat1);"+fechaFin.compareTo(fechaInit));
          Date dat1 = format.parse(fechaInit);
          logger.info("Fecha INicio Formateada: "+dat1);
          Date dat2 = format.parse(fechaFin);
          salida = dat2.compareTo(dat1);
          //salida = fechaFin.compareTo(fechaInit);
  
      } catch (Exception e) {
          logger.error("Error al comparar la fecha metodo utils "
                  + e.getMessage());
          e.printStackTrace();
      }
      return salida;
  }
  </code></pre>
  
  <p>Okay, Now here is my problem:</p>
  
  <p>When I select a date, example: 03/26/2018 as starting date and 03/28/2020 as ending date, when I save it and click edit it, It shows me 03/02/2020 as starting and 03/02/2022 as ending date, WHen I Sout(Syso) thedate I got from form, was this format: 03/26/2018 
  When I parse it, with pattern: "dd/MM/yyyy" it give me a result of: "MMM dd, yyyy hh:mm:ss a"</p>
  
  <p>And then it change the date to about something like more than 6 months from original date, to about 2 years as max.</p>
  
  <p>Any help?</p>