java if语句判断一周中某一天是否为工作日
时间: 2024-09-20 17:15:06 浏览: 60
Java 零基础控制语句 - switch 语句.md
在 Java 中,你可以使用 `java.time` 包下的 `DayOfWeek` 类来判断某一天是否为工作日。通常,工作日是指周一到周五,周六和周日被视为周末。下面是一个简单的示例:
```java
import java.time.DayOfWeek;
import java.time.LocalDate;
public class WorkDayCheck {
public static boolean isWorkingDay(LocalDate date) {
DayOfWeek dayOfWeek = date.getDayOfWeek();
return dayOfWeek == DayOfWeek.MONDAY || dayOfWeek == DayOfWeek.TUESDAY ||
dayOfWeek == DayOfWeek.WEDNESDAY || dayOfWeek == DayOfWeek.THURSDAY ||
dayOfWeek == DayOfWeek.FRIDAY;
}
public static void main(String[] args) {
LocalDate today = LocalDate.now(); // 获取当前日期
if (isWorkingDay(today)) {
System.out.println("今天是工作日");
} else {
System.out.println("今天是周末");
}
}
}
```
在这个例子中,`isWorkingDay` 函数接收一个 `LocalDate` 对象,然后通过比较 `DayOfWeek` 的值来判断这一天是不是工作日。
阅读全文