2

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!

chunyuen2
  • 53
  • 1
  • 6

2 Answers2

1

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);
firegloves
  • 5,581
  • 2
  • 29
  • 50
  • Thanks a lot! I'm just a beginner, so i may need some time to understand the code. – chunyuen2 Jan 05 '17 at 02:14
  • @chunyuen2 Be aware that the `Calendar` class is very confusing and poorly designed. For many reasons it is now supplanted by the `ZonedDateTime` class and other java.time classes. As a beginner, do not look at `Calendar` as an example of good object-oriented programming; it is more an example of what *not* to do. – Basil Bourque Jan 05 '17 at 07:16
  • @BasilBourque Truely, i'm quite confused about it. – chunyuen2 Jan 06 '17 at 03:11
  • hey guys we have used these classes for years. Now there are newer ones, but these are still valid. @chunyuen2 read my code carefully, i'm simply setting the first monday of january then I'm adding 7 days to the calendar instance until the month changes – firegloves Jan 06 '17 at 10:50
  • @firegloves hi my buddy, thanks again for your reply, but i have another question that what if i pick Monday and Wednesday at the same time.. is it possible to write the code in the same class? like this: `cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY+WEDNESDAY);` – chunyuen2 Jan 07 '17 at 18:10
  • @firegloves Also, it seems the `+1` from `output += cal.get(Calendar.DAY_OF_MONTH) + "/" + (cal.get(Calendar.MONTH)+1) + ","; cal.add(Calendar.DAY_OF_MONTH, 7);` is the most important part that determines the month even though it is set`cal.set(Calendar.MONTH, Calendar.JANUARY)` – chunyuen2 Jan 07 '17 at 18:25
  • @chunyuen2 you can't do MONDAY+WEDNESDAY because they are constants for predefined int values. Summing them you are referencing to another int value. then for month: months starts from 0 = JANUARY, 1 = FEBRUARY and so on. So if you want to print human readable values you have to add 1 to obtain your desider output format – firegloves Jan 12 '17 at 14:10
  • @firegloves thanks, i've achieved that by copying the coding and change some variables :) – chunyuen2 Jan 12 '17 at 15:38
0

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.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Thank you so much! I'm so appreciated that you've given such a great and detailed answer. Yet, i am just a beginner, some codes above are quite new for me.. but i will try to figure out what they are. Thanks! – chunyuen2 Jan 05 '17 at 02:17
  • @chunyuen2 You can read the [Oracle Tutorial on Enum](http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html). Enums in Java are much more powerful and useful than simple enums found in other languages. And read [Oracle Tutorial](http://docs.oracle.com/javase/tutorial/collections/intro/) if you are new to collections in general and `Set`/`List` specifically. – Basil Bourque Jan 05 '17 at 07:04
  • @BasilBourquean Can i use this in writing an Android app? As i'm learning to write an Android app by using the Android studio and most of the code are based on the language `java`. – chunyuen2 Jan 06 '17 at 03:09
  • @chunyuen2 Reread my Answer carefully. Notice the bullet "Android". – Basil Bourque Jan 06 '17 at 08:45