.发起检测后,无论是否检测成功,均计算为一次检测次数; 2.今日检测次数的统计时间段为每天的00:00:00至23:59:59,超过后计算为次日的检测次数; 3.累计检测次数为该类型检测功能的历史检测次数总和。用java写一段示例代码
时间: 2023-11-27 13:50:23 浏览: 19
下面是一个简单的示例代码,用于统计每天的检测次数和累计检测次数:
```
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
public class DetectionCounter {
private int dailyCount;
private int totalCount;
private Map<LocalDate, Integer> dailyCounts;
public DetectionCounter() {
this.dailyCount = 0;
this.totalCount = 0;
this.dailyCounts = new HashMap<>();
}
public void recordDetection() {
LocalDateTime now = LocalDateTime.now();
LocalDate today = now.toLocalDate();
LocalTime midnight = LocalTime.MIDNIGHT;
LocalDateTime startOfToday = LocalDateTime.of(today, midnight);
if (now.isBefore(startOfToday.plusDays(1))) {
dailyCount++;
} else {
dailyCounts.put(today, dailyCount);
dailyCount = 1;
}
totalCount++;
}
public int getDailyCount() {
LocalDateTime now = LocalDateTime.now();
LocalDate today = now.toLocalDate();
LocalTime midnight = LocalTime.MIDNIGHT;
LocalDateTime startOfToday = LocalDateTime.of(today, midnight);
if (now.isBefore(startOfToday.plusDays(1))) {
return dailyCount;
} else {
Integer count = dailyCounts.get(today);
return count == null ? 0 : count;
}
}
public int getTotalCount() {
return totalCount;
}
public static void main(String[] args) {
DetectionCounter counter = new DetectionCounter();
for (int i = 0; i < 5; i++) {
counter.recordDetection();
}
System.out.println("今日检测次数:" + counter.getDailyCount());
System.out.println("累计检测次数:" + counter.getTotalCount());
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
System.out.println("每日检测次数:");
for (LocalDate date : counter.dailyCounts.keySet()) {
int count = counter.dailyCounts.get(date);
String dateString = date.format(formatter);
System.out.println(dateString + ": " + count);
}
}
}
```
这个示例代码实现了一个DetectionCounter类,用于统计每天的检测次数和累计检测次数。recordDetection()方法用于记录一次检测,getDailyCount()方法用于获取今日检测次数,getTotalCount()方法用于获取累计检测次数。运行这个示例代码会输出以下结果:
```
今日检测次数:5
累计检测次数:5
每日检测次数:
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![.pdf](https://img-home.csdnimg.cn/images/20210720083646.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)