13

I need to find all the weekend dates for a given month and a given year.

Eg: For 01(month), 2010(year), the output should be : 2,3,9,10,16,17,23,24,30,31, all weekend dates.

Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240
usman
  • 1,947
  • 4
  • 19
  • 19

4 Answers4

21

Here is a rough version with comments describing the steps:

// create a Calendar for the 1st of the required month
int year = 2010;
int month = Calendar.JANUARY;
Calendar cal = new GregorianCalendar(year, month, 1);
do {
    // get the day of the week for the current day
    int day = cal.get(Calendar.DAY_OF_WEEK);
    // check if it is a Saturday or Sunday
    if (day == Calendar.SATURDAY || day == Calendar.SUNDAY) {
        // print the day - but you could add them to a list or whatever
        System.out.println(cal.get(Calendar.DAY_OF_MONTH));
    }
    // advance to the next day
    cal.add(Calendar.DAY_OF_YEAR, 1);
}  while (cal.get(Calendar.MONTH) == month);
// stop when we reach the start of the next month
mikej
  • 65,295
  • 17
  • 152
  • 131
  • sample is not working properly it gives output :2,3,9,10,16,17,23,24,30, but for jan 2010 the week end list are2,3,9,10,16,17,23,24,30,31 . please tell me where is the pro – usman Jul 17 '10 at 17:35
  • There was a bug in the `while` condition where the last day of the month was being missed. I have fixed it now. – mikej Jul 17 '10 at 17:36
  • 1
    FYI, the terribly troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Dec 11 '18 at 03:44
  • This code is just flat out wrong in many locales; not all locales consider 'Saturday and Sunday' the weekend days! – rzwitserloot Jun 21 '21 at 17:54
14

java.time

You can use the Java 8 stream and the java.time package. Here an IntStream from 1 to the number of days in the given month is generated. This stream is mapped to a stream of LocalDate in the given month then filtered to keep Saturday's and Sunday's.

import java.time.DayOfWeek;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.YearMonth;
import java.util.stream.IntStream;

class Stackoverflow{
    public static void main(String args[]){

        int year    = 2010;
        Month month = Month.JANUARY;

        IntStream.rangeClosed(1,YearMonth.of(year, month).lengthOfMonth())
                 .mapToObj(day -> LocalDate.of(year, month, day))
                 .filter(date -> date.getDayOfWeek() == DayOfWeek.SATURDAY ||
                                 date.getDayOfWeek() == DayOfWeek.SUNDAY)
                 .forEach(date -> System.out.print(date.getDayOfMonth() + " "));
    }
}

We find the same result as the first answer (2 3 9 10 16 17 23 24 30 31).

Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240
3

The Answer by Lokni appears to be correct, with bonus points for using Streams.

EnumSet

My suggestion for improvement: EnumSet. This class is an extremely efficient implementation of Set. Represented internally as bit vectors, they are fast to execute and taking very little memory.

Using an EnumSet enables you to soft-code the definition of the weekend by passing in a Set<DayOfWeek>.

Set<DayOfWeek> dows = EnumSet.of( DayOfWeek.SATURDAY , DayOfWeek.SUNDAY );

Demo using the old-fashioned syntax without Streams. You could adapt Lokni’s answer’s code to use an EnumSet in a similar manner.

YearMonth ym = YearMonth.of( 2016 , Month.JANUARY ) ;
int initialCapacity = ( ( ym.lengthOfMonth() / 7 ) + 1 ) * dows.size() ;  // Maximum possible weeks * number of days per week.
List<LocalDate> dates = new ArrayList<>(  initialCapacity  );
for (int dayOfMonth = 1;  dayOfMonth <= ym.lengthOfMonth() ;  dayOfMonth ++) {
    LocalDate ld =  ym.atDay( dayOfMonth ) ;
    DayOfWeek dow = ld.getDayOfWeek() ;
    if( dows.contains( dow ) ) {  
        // Is this date *is* one of the days we care about, collect it.
        dates.add( ld );
    }
}

TemporalAdjuster

You can also make use of the TemporalAdjuster interface which provides for classes that manipulate date-time values. The TemporalAdjusters class (note the plural s) provides several handy implementations.

The ThreeTen-Extra project provides classes working with java.time. This includes a TemporalAdjuster implementation, Temporals.nextWorkingDay().

You can write your own implementation to do the opposite, a nextWeekendDay temporal adjuster.


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.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

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
2

You could try like this:

int year=2016;
int month=10;
calendar.set(year, 10- 1, 1);
int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
ArrayList<Date> sundays = new ArrayList<Date>();>

for (int d = 1;  d <= daysInMonth;  d++) {
      calendar.set(Calendar.DAY_OF_MONTH, d);
      int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
      if (dayOfWeek==Calendar.SUNDAY) {
            calendar.add(Calendar.DATE, d);
            sundays.add(calendar.getTime());
      }
}
MWiesner
  • 8,868
  • 11
  • 36
  • 70
VinayaK
  • 23
  • 7