/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; import java.time.* ; /* Name of the class has to be "Main" only if the class is public. */ class Ideone { { ZoneId z = ZoneId.of( "Africa/Tunis" ) ; YearMonth yearMonthNow = YearMonth.now( z ) ; YearMonth yearMonthJanuaryTwoYearAgo = yearMonthNow.withMonth( 1 ).minusYears( 2 ) ; ArrayList< YearMonth > yearMonths = new ArrayList<>(); YearMonth ym = yearMonthJanuaryTwoYearAgo ; while( ! ym.isAfter( yearMonthNow ) ) { yearMonths.add( ym ) ; // Set up the next loop. ym = ym.plusMonths( 1 ) ; } } }
Standard input is empty
[2017-01, 2017-02, 2017-03, 2017-04, 2017-05, 2017-06, 2017-07, 2017-08, 2017-09, 2017-10, 2017-11, 2017-12, 2018-01, 2018-02, 2018-03, 2018-04, 2018-05, 2018-06, 2018-07, 2018-08, 2018-09, 2018-10, 2018-11, 2018-12, 2019-01, 2019-02, 2019-03, 2019-04, 2019-05, 2019-06]