localdate 获取本月有多少周
时间: 2024-01-15 22:05:25 浏览: 67
C#根据每月日期得到是第几周
3星 · 编辑精心推荐
可以使用Java 8中的java.time包中的类来获取本月有多少周。以下是一个示例代码:
```java
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
public class Main {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
LocalDate firstDayOfMonth = today.with(TemporalAdjusters.firstDayOfMonth());
LocalDate lastDayOfMonth = today.with(TemporalAdjusters.lastDayOfMonth());
int weeksInMonth = lastDayOfMonth.get(ISOFields.WEEK_OF_WEEK_BASED_YEAR) - firstDayOfMonth.get(ISOFields.WEEK_OF_WEEK_BASED_YEAR) + 1;
System.out.println("本月有 " + weeksInMonth + " 周");
}
}
```
输出结果将显示本月有多少个完整的周。
阅读全文