title: Infinite loop trying to iterate through dates in Java
body: <p>I'm trying to iterate through dates without success as an inifite loop happens and it won't stop creating records in the database. Why?</p>
  
  <p>I have the following method which increments by one the date passed as a parameter and then return it updated.</p>
  
  <pre><code>public static Date addDays(Date date, int days) {
      Calendar cal = Calendar.getInstance();
      cal.setTime(date);
      cal.add(Calendar.DATE, days);
      return cal.getTime();
  }
  </code></pre>
  
  <p>And then I use it in a for-loop this way...</p>
  
  <pre><code>for (Date initDate = fromDate; initDate.before(toDate); addDays(initDate, 1)) {
      // Do something...
  }
  </code></pre>
  
  <p>fromDate and toDate are values from a JTextField parsed into sql.Date this way.</p>
  
  <pre><code>private java.sql.Date fechaParser(String f) {
      SimpleDateFormat formatDate = new SimpleDateFormat("dd/MM/yyyy");
      Date date;
  
      try {
          date = formatDate.parse(f);
          java.sql.Date sqlDate = new java.sql.Date(date.getTime());
  
          return sqlDate;
      } catch (ParseException e) {
          JOptionPane.showMessageDialog(null, "Wrong date format.");
          e.printStackTrace();
      }
  
      return null;
  }
  </code></pre>