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 toda
Calculate the number of weeks between x and y, and take x's weekday into account.
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]
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.
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.