Home > database >  How to find first business day of month in Java?
How to find first business day of month in Java?

Time:11-06

Let's say I have the current day, the next day, and the previous day. And I want to write a method that finds the first business day of the month by doing logical operations on them. For example, the first working day of November 2021 starts on the 4th, because the 3rd of the month has no working days due to holidays. In short, we need to write a program that takes into account non-working days and finds the first working day of each month.

CodePudding user response:

Well, there is no standard Java library to get the dates of the holidays. That would be too localized, because holidays heavily depend on your country and region (except for widely known holidays, such as Christmas or Easter).

You must rely on some external source, for instance a holiday API. Once you have the holidays, you could easily get the first business day of a month.

In the code below, I've hardcoded the holidays as a Set of LocalDates. I also assumed that business days are from Monday to Friday.

public static Optional<LocalDate> firstBusinessDayOfMonth(YearMonth month) {

    // I've hardcoded the holidays as LocalDates
    // and put them in a Set
    final Set<LocalDate> holidays = Set.of(
        LocalDate.of(2021, 11, 1),
        LocalDate.of(2021, 11, 2),
        LocalDate.of(2021, 11, 3)
    );
    // For the sake of efficiency, I also put the business days into a Set.
    // In general, a Set has a better lookup speed than a List.
    final Set<DayOfWeek> businessDays = EnumSet.of(
        MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY
    );

    return

        // All dates of the month
        month.atDay(1).datesUntil(month.plusMonths(1).atDay(1))

            // Retain all business days. Use static imports from
            // java.time.DayOfWeek.*
            .filter(date -> businessDays.contains(date.getDayOfWeek()))

            // Retain only dates not present in our holidays list
            .filter(date -> !holidays.contains(date))

            // Get the first
            .findFirst();
}

To get the first business day of the current month, use firstBusinessDayOfMonth(YearMonth.now()).

Note that this method returns an Optional, because it could theoretically be the case that there is no business day within the whole month.

CodePudding user response:

Thanks for all the answers and help, but this is what I wanted.

Here, in my isOperateDay variable, they say that the data they have already sent me from the other server is 1 business day, not 0 business day. What I just wanted to do was to check if the values between yesterday and today are the first working day. As you can see, I'm taking yesterday's month and comparing it to today's month, if yesterday's month is different, it means it was the previous month and my current day's month is counted as the first working day.

@Data
class ST_XF_DWH {
    private LocalDateTime previousDate;
    private LocalDateTime currentDate;
    private int isOperateDate;
}

    public void isFirstWorkDay(ST_XF_DWH st_xf_dwh) {

        LocalDateTime previousDate = st_xf_dwh.getPreviousDate();

        LocalDateTime currentDate = st_xf_dwh.getCurrentDate();

        if (previousDate.getMonth() != currentDate.getMonth() && st_xf_dwh.getIsOperateDate() == 1) {
            System.out.println("Is First Work Day"   st_xf_dwh.getCurrentDate());
        } else {
            System.out.println(st_xf_dwh.getCurrentDate()   "is not First Work Day");
        }

    }
  • Related