fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. import java.time.* ;
  8.  
  9. /* Name of the class has to be "Main" only if the class is public. */
  10. class Ideone
  11. {
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14.  
  15. ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
  16. YearMonth yearMonthNow = YearMonth.now( z ) ;
  17.  
  18. YearMonth yearMonthJanuaryTwoYearAgo = yearMonthNow.withMonth( 1 ).minusYears( 2 ) ;
  19.  
  20. ArrayList< YearMonth > yearMonths = new ArrayList<>();
  21. YearMonth ym = yearMonthJanuaryTwoYearAgo ;
  22. while( ! ym.isAfter( yearMonthNow ) )
  23. {
  24. yearMonths.add( ym ) ;
  25. // Set up the next loop.
  26. ym = ym.plusMonths( 1 ) ;
  27. }
  28.  
  29. System.out.println( yearMonths ) ;
  30.  
  31. }
  32. }
Success #stdin #stdout 0.17s 2184192KB
stdin
Standard input is empty
stdout
[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]