title: Java How to use for loop to populate Year and Month list?
body: <p>How can I loop year and months to display below output?
  Limit of the period to show is current month and year
  Also, show 3 years inclusive of the year as of date. Meaning if its 2019 now, show 2018 and 2017.</p>
  
  <p>I tried with some code to run as java application in hope to get the expected output below but this is what I've tried and got.</p>
  
  <p>Would appreciate much if anyone can shed some light here.</p>
  
  <pre><code>public class TestClass {
  public static void main(String[] args) {
      Calendar today = Calendar.getInstance();
      //month=5, index starts from 0
      int month = today.get(Calendar.MONTH);
      //year=2019
      int year = today.get(Calendar.YEAR);
  
      for(int i = 0; i &lt; 3; i++) {    //year
          for(int j= 0; j &lt;= month; j++) {    //month
          System.out.println("Value of year: " + (year - 2)); //start from 2017 and iterate to 2 years ahead
          System.out.println("Value of month: " + (month + 1)); //start from January (index val: 1) and iterate to today's month
          }
      }
  }
  </code></pre>
  
  <p>}    </p>
  
  <p><strong>Expected Output:</strong></p>
  
  <p>2017
  1
  2017
  2
  2017
  3
  2017
  4
  2017
  5
  2017
  6
  2017
  7
  2017
  8
  2017
  9
  2017
  10
  2017
  11
  2017
  12</p>
  
  <p>2018
  1
  2018
  2
  2018
  3
  2018
  4
  2018
  5
  2018
  6
  2018
  7
  2018
  8
  2018
  9
  2018
  10
  2018
  11
  2018
  12</p>
  
  <p>2019
  1
  2019
  2
  2019
  3
  2019
  4
  2019
  5
  2019
  6</p>