其实时间是2021-08-01,结束时间是2022-05-03,用Java语言
时间: 2023-08-10 20:30:40 浏览: 74
好的,可以使用Java 8中的LocalDate和TemporalAdjusters工具类来实现。代码如下:
```java
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
public class FridayCounter {
public static int countFridays(LocalDate startDate, LocalDate endDate) {
int fridayCount = 0;
LocalDate nextFriday = startDate.with(TemporalAdjusters.next(DayOfWeek.FRIDAY));
while (nextFriday.isBefore(endDate)) {
fridayCount++;
nextFriday = nextFriday.with(TemporalAdjusters.next(DayOfWeek.FRIDAY));
}
return fridayCount;
}
public static void main(String[] args) {
LocalDate startDate = LocalDate.parse("2021-08-01");
LocalDate endDate = LocalDate.parse("2022-05-03");
int fridayCount = countFridays(startDate, endDate);
System.out.println("There are " + fridayCount + " Fridays between " + startDate + " and " + endDate);
}
}
```
运行结果:
```
There are 39 Fridays between 2021-08-01 and 2022-05-03
```
这个代码会计算出指定时间段内的所有周五,并返回周五的数量。
阅读全文