I've made a spinner for selecting a certain month, then I've also created some checkbox for choosing certain days, then how can i show all the dates after choosing such information?
E.g. If i choose January and pick Mondays and Wednesdays, how can i show all the dates of Mondays: 2/1,9/1/,16/1,23/1,30/1 and Wednesdays:4/1,11/1,18/1,25/1 respectively?
I'm looking forward to hearing from you! Thanks!
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.
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.
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?
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.