Calculate previous 12 months from given month - SimpleDateFormat
<p>I am trying to get previous 12 month into an arraylist, from the given month(taken from DB).</p>

<pre><code>List&lt;String&gt; allDates = new ArrayList&lt;String&gt;();    

sqlQuery="select max(date) from Table_Name";

maxDate="Jan-2016"; (Result from Query);
</code></pre>

<p>To get previous 12 months from maxDate,where i use SimpleDateFormat.</p>

<p>I want to calculate the previous 12 months from <strong>Given Month</strong> (maxDate), not from current month, i tried the following code.</p>

<pre><code>//  Parsing maxDate to an integer (say Jan-2016 = 0, Feb-2016= 1)
Date date = new SimpleDateFormat("MMM-yyyy").parse(maxDate);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int month=cal.get(Calendar.MONTH);
        System.out.println("month : "+month);

// Looping to get previous 12 months from current month.
SimpleDateFormat month_date = new SimpleDateFormat("MMM-yyyy");
          for (int i = 12; i &gt; 0; i--) {
                Calendar calendar1 = Calendar.getInstance();
                calendar1.add(Calendar.MONTH, -i);
                String month_name1 = month_date.format(calendar1.getTime());
                allDates.add(month_name1);
            }
            System.out.println(allDates);
</code></pre>

<p>Since months are numbered from <strong>(0 - 11)</strong> i couldn't achieve it.
Please suggest an idea to calculate previous 12 months from given month. Appreciate your help!</p>