4

I want to get some days between a given range of dates. What would be an optimal solution for this? For example, I want to get all Monday, Wednesday and Thursday dates from today up to two months.

hoijui
  • 3,615
  • 2
  • 33
  • 41
George Artemiou
  • 3,076
  • 2
  • 19
  • 36

3 Answers3

3

Using java.time

The modern approach uses the java.time classes.

Instantiate List objects to collect your results.

List<LocalDate> mondays = new ArrayList<>() ;
List<LocalDate> wednesdays = new ArrayList<>() ;
List<LocalDate> thursdays = new ArrayList<>() ;

Determine today's date. This requires a time zone, as the date varies around the globe by zone for any given moment.

LocalDate today = LocalDate.now( ZoneId.of( "America/Montreal" ) ) ;

Add two more months. The java.time classes smartly handle the issue of shorter/longer months. Read the class doc to see if you agree with their behavior/policies.

LocalDate twoMonthsLater = today.plusMonths( 2 );

Loop each date, incrementing one day at a time. Test its day-of-week value using the DayOfWeek enum. Note that you can switch on an Enum object, but oddly cannot include its class name prefix. So we use MONDAY rather than my preferred DayOfWeek.MONDAY.

LocalDate localDate = today ;
while ( localDate.isBefore( twoMonthsLater ) ) {
    DayOfWeek dow = localDate.getDayOfWeek() ;
     switch ( dow ) {
            case MONDAY: 
                mondays.add( localDate ) ;
                break;

            case WEDNESDAY: 
                wednesdays.add( localDate ) ;
                break;

            case THURSDAY:
                thursdays.add( localDate ) ;
                break;

            default: 
                // Ignore any other day-of-week.
                break;
        }
        // Set-up the next loop. Increment by one day at a time.
        localDate = localDate.plusDays( 1 ) ;
}

Dump to console.

System.out.println( "From " + today + " to " + twoMonthsLater + ":"  );
System.out.println( "mondays: " + mondays ) ;
System.out.println( "wednesdays: " + wednesdays ) ;
System.out.println( "thursdays: " + thursdays ) ;

See this code run live at IdeOne.com.

From 2017-10-17 to 2017-12-17:

mondays: [2017-10-23, 2017-10-30, 2017-11-06, 2017-11-13, 2017-11-20, 2017-11-27, 2017-12-04, 2017-12-11]

wednesdays: [2017-10-18, 2017-10-25, 2017-11-01, 2017-11-08, 2017-11-15, 2017-11-22, 2017-11-29, 2017-12-06, 2017-12-13]

thursdays: [2017-10-19, 2017-10-26, 2017-11-02, 2017-11-09, 2017-11-16, 2017-11-23, 2017-11-30, 2017-12-07, 2017-12-14]


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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • How counting weeks result in counting a specific DayOfWeek? `long mondayCount = ChronoUnit.WEEKS.between( 01/10/2017, 31/10/2017 );` I just tested and mondayCount = 4 for OCT/2017, which has 5 Mondays, – FiruzzZ Oct 17 '17 at 17:29
  • @FiruzzZ You really should open a Question for a tangent, but… To quote the JavaDoc: *For the ISO calendar system, it is equal to 7 days.*. So ( 31 / 7 ) = 4.4…, which means four (4) full weeks. The term “week” is quite ambiguous, so always read the documentation carefully to see if your meaning lines up with their meaning. If you want standard ISO 8601 Monday-based weeks, see the week-based year classes `IsoFields` in Java 8 & 9, and [`YearWeek`](http://www.threeten.org/threeten-extra/apidocs/org/threeten/extra/YearWeek.html) in the *ThreeTen-Extra* project. – Basil Bourque Oct 17 '17 at 18:25
1

Calculate the number of weeks between x and y, and take x's weekday into account.

Arne
  • 2,106
  • 12
  • 9
0

Have a look at java.util.Calendar. One can get a instance of it, set it to a given date, then, for example, call get(DAY_OF_WEEK) on it. Also it has some nice methods to get number of day in a given year and number of days in a given year.

david a.
  • 5,283
  • 22
  • 24