title: How to get list of months between two dates in Java
body: <p>I am trying to get a list of months (actually the first days of those months) between two dates in Java but I am not getting the expected results.</p>
  <p>The start date is &quot;3/17/2020&quot;, the end date &quot;3/17/2021&quot; and the expected result is as follows:</p>
  <pre><code>&quot;01-Mar-2020&quot;
  &quot;01-Apr-2020&quot;
  &quot;01-May-2020&quot;
  &quot;01-Jun-2020&quot;
  &quot;01-Jul-2020&quot;
  &quot;01-Aug-2020&quot;
  &quot;01-Sep-2020&quot;
  &quot;01-Oct-2020&quot;
  &quot;01-Nov-2020&quot;
  &quot;01-Dec-2020&quot;
  &quot;01-Jan-2021&quot;
  &quot;01-Feb-2021&quot;
  &quot;01-Mar-2021&quot;
  </code></pre>
  <p>Here below is the code I am using:</p>
  <pre><code>import java.text.DateFormat;
  import java.text.ParseException;
  import java.text.SimpleDateFormat;
  import java.util.Calendar;
  
  public class Main {
     public static void main(String[] args) {
         String date1 = &quot;3/17/2020&quot;;
         String date2 = &quot;3/17/2021&quot;;
  
      DateFormat formater = new SimpleDateFormat(&quot;MM/dd/yyyy&quot;);
  
      Calendar beginCalendar = Calendar.getInstance();
      Calendar finishCalendar = Calendar.getInstance();
  
      try {
          beginCalendar.setTime(formater.parse(date1));
          finishCalendar.setTime(formater.parse(date2));
      } catch (ParseException e) {
          e.printStackTrace();
      }
    DateFormat formaterYd = new SimpleDateFormat(&quot;01-MMM-YYYY&quot;);
  
  
      while (beginCalendar.before(finishCalendar)) {
          // add one month to date per loop
          String date =    formaterYd.format(beginCalendar.getTime()).toUpperCase();
          System.out.println(date);
          beginCalendar.add(Calendar.MONTH, 1);
         }
     }
  }
  </code></pre>
  <p>With the above code I am getting the following result:</p>
  <pre><code>&quot;01-Jan-2020&quot;
  &quot;01-Feb-2020&quot;
  &quot;01-Mar-2020&quot;
  &quot;01-Apr-2020&quot;
  &quot;01-May-2020&quot;
  &quot;01-Jun-2020&quot;
  &quot;01-Jul-2020&quot;
  &quot;01-Aug-2020&quot;
  &quot;01-Sep-2020&quot;
  &quot;01-Oct-2020&quot;
  &quot;01-Nov-2020&quot;
  &quot;01-Dec-2020&quot;
  </code></pre>
  <p>Please help me understand the issue and suggest any solution for the same with java 7.</p>