java.time
It is much easier now with the modern java.time classes that supplant the unpleasant old time and time classes discussed here in Question and other answers.
The java.time framework is built into Java 8 and later. These classes supersede old inconvenient time classes such as java.util.Date , .Calendar and java.text.SimpleDateFormat .
Now, in Joda-Time maintenance mode, the project also advises switching to java.time.
To learn more, see the Oracle Tutorial . And search for qaru for many examples and explanations.
Most of the functionality of java.time is ported back to Java 6 and 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP .
The ThreeTen-Extra project extends java.time with additional classes. This project is proof of possible future additions to java.time.
YearMonth
The YearMonth class provides only what you want.
YearMonth start = YearMonth.of( 2008 , Month.OCTOBER ); YearMonth stop = YearMonth.of( 2009 , Month.DECEMBER ); List<YearMonth> yms = new ArrayList<>(); YearMonth ym = start ; while( ! ym.isAfter( stop ) ) { yms.add( ym ); // Set up the next loop. ym = ym.plusMonths( 1 ); }
To represent, use DateTimeFormatter to create a string representation of the value of YearMonth .
DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MM MMM" ); f = f.withLocale( Locale.CANADA_FRENCH );
To retrieve the last day of the month, YearMonth object.
LocalDate endOfMonth = ym.atEndOfMonth();
To submit, use a DateTimeFormatter . Either let it create an instance of formatting that is automatically localized according to the specified Locale or specify your own formatting template. Shown many times in many other stack overflow questions and answers.