Avoid legacy date-time classes
The old Date
and Calendar
classes have proven to be confusing, flawed, and troublesome. Now legacy, supplanted by the java.time classes in Java 8 and later. See below for links to back-ports for earlier Java and for Android.
Using java.time
The data behind your which-days-of-the-week checkboxes can be represented using the DayOfWeek
enum. That class provides seven instances, one for each day of the week, Monday-Sunday.
You can collect the user’s desired days in a EnumSet
, a special highly-optimized Set
implementation for Enum
objects.
Set<DayOfWeek> daysOfWeek = EnumSet.noneOf( DayOfWeek.class );
daysOfWeek.add( DayOfWeek.MONDAY );
daysOfWeek.add( DayOfWeek.WEDNESDAY );
Remember the chosen month and year using the YearMonth
object.
YearMonth ym = YearMonth.of( 2017 , Month.JANUARY ); // Or ( 2017 , 1 ) for January.
Get the first date of that month, represented as a LocalDate
. The LocalDate
class represents a date-only value without time-of-day and without time zone.
LocalDate firstOfMonth = ym.atDay( 1 );
Loop each day-of-week in our collection. For each, determine the first occurrence of the month. Java provides a handy TemporalAdjuster
implementation found in TemporalAdjusters
for that first occurrence. Then add a week at a time until we find ourselves in the next month.
for ( DayOfWeek dayOfWeek : daysOfWeek ) {
List<LocalDate> dates = new ArrayList<> ( 5 );
LocalDate ld = firstOfMonth.with ( TemporalAdjusters.dayOfWeekInMonth ( 1 , dayOfWeek ) );
do {
dates.add ( ld );
// Set up next loop.
ld = ld.plusWeeks ( 1 );
} while ( YearMonth.from ( ld ).equals ( ym ) ); // While in the targeted month.
System.out.println ( "ym.toString(): " + ym + " | dayOfWeek.toString(): " + dayOfWeek + " | dates: " + dates );
}
ym.toString(): 2017-01 | dayOfWeek.toString(): MONDAY | dates: [2017-01-02, 2017-01-09, 2017-01-16, 2017-01-23, 2017-01-30]
ym.toString(): 2017-01 | dayOfWeek.toString(): WEDNESDAY | dates: [2017-01-04, 2017-01-11, 2017-01-18, 2017-01-25]
See this code live in IdeOne.com.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
- Java SE 8 and SE 9 and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.