Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find Saturdays and Sundays in A given month?

I want find all Saturdays and Sundays in A given month. How can I do so?

like image 515
Venkat Avatar asked Mar 28 '12 14:03

Venkat


People also ask

How many Sundays and Saturdays are in a month?

First Day of the Month A quick look at a calendar will show that the first 3 weekdays of any 31-day month will be repeated 5 times in that month. So, any month that has 31 days and begins on a Friday will have 5 Fridays, 5 Saturdays, and 5 Sundays.

How do you calculate the number of Sundays in a year?

The number is generally 52. If the first day of the year is a Sunday, it's 53. if it is a leap year, and the first day of the year is a Saturday, it is 53.

How many weekends are in a year?

Weekend means Saturday & Sunday together. In total we have 52 weeks in a year. So there are 52 weekends in a year.


1 Answers

The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.

Also, quoted below is a notice at the Home Page of Joda-Time:

Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project.

Solution using java.time, the modern Date-Time API:

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.temporal.TemporalAdjusters;
import java.util.List;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        // Test
        System.out.println(getWeekends(2));// All weekends of Feb in the current year
        System.out.println(getWeekends(2020, 2));// All weekends of Feb 2020
    }

    /*
     * All weekends (Sat & Sun) of the given month in the current year
     */
    static List<LocalDate> getWeekends(int month) {
        LocalDate firstDateOfTheMonth = LocalDate.now().withMonth(month).with(TemporalAdjusters.firstDayOfMonth());
        
        return firstDateOfTheMonth
                .datesUntil(firstDateOfTheMonth.plusMonths(1))
                .filter(date -> date.getDayOfWeek() == DayOfWeek.SATURDAY || date.getDayOfWeek() == DayOfWeek.SUNDAY)
                .collect(Collectors.toList());
    }

    /*
     * All weekends (Sat & Sun) of the given year and the month
     */
    static List<LocalDate> getWeekends(int year, int month) {
        LocalDate firstDateOfTheMonth = YearMonth.of(year, month).atDay(1);
        
        return firstDateOfTheMonth
                .datesUntil(firstDateOfTheMonth.plusMonths(1))
                .filter(date -> date.getDayOfWeek() == DayOfWeek.SATURDAY || date.getDayOfWeek() == DayOfWeek.SUNDAY)
                .collect(Collectors.toList());
    }
}

Output:

[2021-02-06, 2021-02-07, 2021-02-13, 2021-02-14, 2021-02-20, 2021-02-21, 2021-02-27, 2021-02-28]
[2020-02-01, 2020-02-02, 2020-02-08, 2020-02-09, 2020-02-15, 2020-02-16, 2020-02-22, 2020-02-23, 2020-02-29]

ONLINE DEMO

Non-Stream solution:

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        // Test
        System.out.println(getWeekends(2));// All weekends of Feb in the current year
        System.out.println(getWeekends(2020, 2));// All weekends of Feb 2020
    }

    /*
     * All weekends (Sat & Sun) of the given month in the current year
     */
    static List<LocalDate> getWeekends(int month) {
        LocalDate firstDateOfTheMonth = LocalDate.now().withMonth(month).with(TemporalAdjusters.firstDayOfMonth());
        List<LocalDate> list = new ArrayList<>();

        for (LocalDate date = firstDateOfTheMonth; !date
                .isAfter(firstDateOfTheMonth.with(TemporalAdjusters.lastDayOfMonth())); date = date.plusDays(1))
            if (date.getDayOfWeek() == DayOfWeek.SATURDAY || date.getDayOfWeek() == DayOfWeek.SUNDAY)
                list.add(date);

        return list;
    }

    /*
     * All weekends (Sat & Sun) of the given year and the month
     */
    static List<LocalDate> getWeekends(int year, int month) {
        LocalDate firstDateOfTheMonth = YearMonth.of(year, month).atDay(1);
        List<LocalDate> list = new ArrayList<>();

        for (LocalDate date = firstDateOfTheMonth; !date
                .isAfter(firstDateOfTheMonth.with(TemporalAdjusters.lastDayOfMonth())); date = date.plusDays(1))
            if (date.getDayOfWeek() == DayOfWeek.SATURDAY || date.getDayOfWeek() == DayOfWeek.SUNDAY)
                list.add(date);

        return list;
    }
}

ONLINE DEMO

Learn more about the modern Date-Time API from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

like image 105
Arvind Kumar Avinash Avatar answered Oct 23 '22 13:10

Arvind Kumar Avinash