请使用java,帮我写一个,在一天时间内,对一个接口的被调用次数的,第二天自动重新计数java程序
时间: 2024-02-26 07:56:27 浏览: 39
好的,以下是一个简单的Java程序,可以在一天时间内记录接口的被调用次数,并在第二天自动重新计数:
```java
import java.time.*;
import java.util.*;
public class APICounter {
private static int count = 0;
private static LocalDate currentDate;
public static synchronized void increment() {
LocalDate now = LocalDate.now();
if (!now.equals(currentDate)) {
currentDate = now;
count = 0;
}
count++;
}
public static synchronized int getCount() {
LocalDate now = LocalDate.now();
if (!now.equals(currentDate)) {
currentDate = now;
count = 0;
}
return count;
}
public static void main(String[] args) {
// 模拟接口被调用
for (int i = 0; i < 10; i++) {
increment();
}
// 获取接口被调用次数
int currentCount = getCount();
System.out.println("接口被调用次数: " + currentCount);
// 模拟等待一天
try {
Thread.sleep(Duration.ofDays(1).toMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
// 获取接口被调用次数
currentCount = getCount();
System.out.println("接口被调用次数: " + currentCount);
}
}
```
这个程序使用了一个静态变量 `count` 来记录接口被调用的次数,以及一个 `currentDate` 变量来记录当前日期。在每次接口被调用时,程序会检查当前日期是否与 `currentDate` 相同,如果不同则将 `count` 重置为 0,并将 `currentDate` 更新为当前日期。然后程序会将 `count` 加 1。在获取接口被调用次数时,程序也会进行同样的检查和更新操作。
在程序的主函数中,我们模拟了接口被调用了 10 次,然后获取接口被调用次数,并输出结果。接着,我们等待一天的时间,再次获取接口被调用次数,并输出结果。在输出结果时,您可以将其替换为将数据写入数据库或文件等操作。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)