Android: how to get all the dates of the month after selecting the certain month and weekdays?

2k views Asked by At

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!

2

There are 2 answers

4
Basil Bourque On BEST ANSWER

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?

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.

8
firegloves On

With this method you can

private String getMondaysOfJanuary() 

        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.MONTH, Calendar.JANUARY); // month starts by 0 = january
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        cal.set(Calendar.DAY_OF_WEEK_IN_MONTH, 1);
        int month = cal.get(Calendar.MONTH);
        String output = "";
        while (cal.get(Calendar.MONTH) == month) {
            output += cal.get(Calendar.DAY_OF_MONTH) + "/" + (cal.get(Calendar.MONTH)+1) + ",";
            cal.add(Calendar.DAY_OF_MONTH, 7);
        }
        return output.substring(0, output.length-1);
}

Replacing this two lines with yours data and parametrizing this method you can achieve your goal

cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
cal.set(Calendar.DAY_OF_WEEK_IN_MONTH, 1);