Java – last working day of last month and localdate

public static String getLastWorkingDayOfPrevIoUsMonth() {
public static String getLastWorkingDayOfPrevIoUsMonth() {

    LocalDate lastDayOfCurrentMonth = LocalDate.Now().with(TemporalAdjusters.lastDayOfMonth());
    LocalDate lastWorkingDayOfMonth;

    switch (DayOfWeek.of(lastDayOfCurrentMonth.get(ChronoField.DAY_OF_WEEK))) {
        case SATURDAY:
            lastWorkingDayOfMonth = lastDayOfCurrentMonth.minusMonths(1);
            break;
        case SUNDAY:
            lastWorkingDayOfMonth = lastDayOfCurrentMonth.minusMonths(2);
            break;
        default:
            lastWorkingDayOfMonth = lastDayOfCurrentMonth;
    }

    return getFormattedDate(lastWorkingDayOfMonth);
}

The above is the last working day of this month How to adjust the last working day of the previous month?

Solution

Sometimes a simple approach is better than a smart one:

LocalDate lastWorkingDayOfMonth = LocalDate.Now().withDayOfMonth(1);
do {
    lastWorkingDayOfMonth = lastWorkingDayOfMonth.minusDays(1);
} while (lastWorkingDayOfMonth.getDayOfWeek() == DayOfWeek.SATURDAY ||
         lastWorkingDayOfMonth.getDayOfWeek() == DayOfWeek.SUNDAY);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>