You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Java的LocalDate获取一个月中的工作日天数

用Java的LocalDate计算一个月内的工作日天数

嘿,别担心,java.time库其实比你想的要直观,我来一步步教你怎么用它计算一个月的工作日(周一至周五)天数,完全不用依赖任何外部库。

核心思路

咱们要做的就是:

  • 获取指定年月的第一天和最后一天
  • 遍历当月的每一天,判断日期是否属于周一到周五
  • 统计符合条件的天数

代码实现(新手友好版)

这个方法用了最直观的遍历方式,容易理解,适合刚接触java.time的你:

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;

public class WorkdayCounter {
    public static int countWorkdaysInMonth(int year, int month) {
        // 获取当月第一天:java.time的月份是1-12,不用像旧库那样减1,超省心
        LocalDate firstDay = LocalDate.of(year, month, 1);
        // 用TemporalAdjusters快速拿到当月最后一天,不用自己算每个月有多少天
        LocalDate lastDay = firstDay.with(TemporalAdjusters.lastDayOfMonth());
        
        int workdayCount = 0;
        LocalDate currentDay = firstDay;
        
        // 遍历当月所有日期,直到超过最后一天为止
        while (!currentDay.isAfter(lastDay)) {
            DayOfWeek dayOfWeek = currentDay.getDayOfWeek();
            // 判断是不是周六或周日,不是的话就算工作日
            if (dayOfWeek != DayOfWeek.SATURDAY && dayOfWeek != DayOfWeek.SUNDAY) {
                workdayCount++;
            }
            // 日期往后推一天,进入下一次循环
            currentDay = currentDay.plusDays(1);
        }
        
        return workdayCount;
    }

    public static void main(String[] args) {
        // 验证你给出的示例
        System.out.println("2018年4月工作日天数:" + countWorkdaysInMonth(2018, 4)); // 输出21
        System.out.println("2018年5月工作日天数:" + countWorkdaysInMonth(2018, 5)); // 输出23
    }
}

关键知识点解释

  • LocalDate:java.time里用来表示“日期”的类,只包含年、月、日,没有时间部分,非常适合处理这类日期计算。
  • TemporalAdjusters.lastDayOfMonth():这是一个日期调整器,能帮我们直接获取当月的最后一天,不用手动判断每个月是30天还是31天(甚至2月的闰年情况也不用管)。
  • DayOfWeek:枚举类,包含了周一到周日的所有星期值,用它判断星期几比用数字更清晰,不容易出错。

进阶高效版(不用遍历)

如果你需要处理大量日期计算,不想用循环遍历,可以用数学方法直接计算周末数量,再用总天数减去周末数:

public static int countWorkdaysInMonthEfficient(int year, int month) {
    LocalDate firstDay = LocalDate.of(year, month, 1);
    LocalDate lastDay = firstDay.with(TemporalAdjusters.lastDayOfMonth());
    
    int totalDays = lastDay.getDayOfMonth();
    DayOfWeek firstWeekday = firstDay.getDayOfWeek();
    DayOfWeek lastWeekday = lastDay.getDayOfWeek();
    
    int weekendCount = 0;
    
    // 统计当月开头的周末天数
    if (firstWeekday == DayOfWeek.SATURDAY) {
        weekendCount += 1;
    } else if (firstWeekday == DayOfWeek.SUNDAY) {
        weekendCount += 1;
    }
    
    // 统计当月结尾的周末天数
    if (lastWeekday == DayOfWeek.SATURDAY) {
        weekendCount += 1;
    } else if (lastWeekday == DayOfWeek.SUNDAY) {
        weekendCount += 1;
    }
    
    // 统计完整周的周末数量(每个完整周有2天周末)
    int fullWeeks = (totalDays - firstWeekday.getValue() + 1 - (7 - lastWeekday.getValue())) / 7;
    weekendCount += fullWeeks * 2;
    
    return totalDays - weekendCount;
}

这个方法的效率更高,不过逻辑稍微复杂一点,适合对java.time有一定了解后使用。

补充说明

如果之后需要考虑节假日,只需要在判断工作日的逻辑里加上“排除节假日”的条件就行——比如把所有节假日的LocalDate存在一个Set里,然后判断!holidays.contains(currentDay)即可。

内容的提问来源于stack exchange,提问作者Kyu Vulpes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:30:27