Search code examples
javaswingdatejdatechooser

Get all specific week days within the range of two date choosers and 7 checkboxes


I have 2 JDateChooser controls and 7 JCheckBox controls. The date choosers will set a range between two dates, and the 7 checkboxes will filter the dates.

The 7 checkboxes are: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday

For example:

  • I select Oct 01, 2021, from the first date chooser.
  • I select Oct 08, 2021, from the second date chooser.
  • I select Monday and Tuesday from the checkboxes.

The output will be all Mondays and Tuesdays in the dates range Oct 01, 2021 - Oct 08, 2021.

I searched everywhere but no answer and I don't even know where to begin.


Solution

  • Obviously schoolwork, so I’ll be brief, enough to point you in the right direction while letting you actually do your own assignment yourself.

    Seems obvious that you would use checkbox widgets for the days of the week, not a combo box.

    Use LocalDate class for the date. Use the plusDays method to move from one date to another. Test each using getDayOfWeek to match against DayOfWeek objects. Use EnumSet and its contains method for that check.

    Use an ArrayList< LocalDate > to collect the dates you want to remember.

    Make the finished list unmodifiable with a call to List.copyOf.

    Report the values of the collected LocalDate objects by using DateTimeFormatter. To automatically localize, use .ofLocalizedDate.


    In advanced Java, we might do something like this untested code.

    List< LocalDate > dates = 
        LocalDate.of( 2021 , 10 , 1 )
        .datesUntil( LocalDate.of( 2021 , 10 , 8 ) )
        .filter( 
            localDate -> EnumSet.of( DayOfWeek.TUESDAY , DayOfWeek.THURSDAY ).contains( localDate.getDayOfWeek() )
        )
        .toList() 
    ;