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!
Avoid legacy date-time classes
The old
Date
andCalendar
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-optimizedSet
implementation forEnum
objects.Remember the chosen month and year using the
YearMonth
object.Get the first date of that month, represented as a
LocalDate
. TheLocalDate
class represents a date-only value without time-of-day and without time zone.Loop each day-of-week in our collection. For each, determine the first occurrence of the month. Java provides a handy
TemporalAdjuster
implementation found inTemporalAdjusters
for that first occurrence. Then add a week at a time until we find ourselves in the next month.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?
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.