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( "America/Edmonton" ) ;
  16. YearMonth currentYm = YearMonth.now( z ) ;
  17.  
  18. List< YearMonth > yms = new ArrayList<>() ;
  19. int limit = 3 ; // We want current month plus three previous months.
  20. for( int i = 0 ; i <= limit ; i ++ ) {
  21. YearMonth ym = currentYm.minusMonths( i ) ;
  22. yms.add( ym ) ;
  23. }
  24.  
  25. for( YearMonth ym : yms ) {
  26. System.out.println( "YearMonth: " + ym + " starts: " + ym.atDay( 1 ) + " ends: " + ym.atEndOfMonth() ) ;
  27. }
  28.  
  29.  
  30. }
  31. }
Success #stdin #stdout 0.15s 2184192KB
stdin
Standard input is empty
stdout
YearMonth: 2019-04 starts: 2019-04-01 ends: 2019-04-30
YearMonth: 2019-03 starts: 2019-03-01 ends: 2019-03-31
YearMonth: 2019-02 starts: 2019-02-01 ends: 2019-02-28
YearMonth: 2019-01 starts: 2019-01-01 ends: 2019-01-31