JAVA 1.传入设备工作时段字符串,格式为String(HH:mm-HH:mm;HH:mm-HH:mm;HH:mm-HH:mm), 2.传入设备工作开始日期时间(Date类型yyyy-MM-dd hh:mm:ss) 3.传入设备工作结束日期时间(Date类型yyyy-MM-dd hh:mm:ss) 4.计算这台设备这期间每天的工作时长(分钟)
时间: 2024-02-05 10:13:03 浏览: 166
可以按照以下步骤来实现这个需求:
1. 将设备工作时段字符串解析成一个列表,每个元素表示一段工作时段,每个时段包含一个开始时间和一个结束时间。
2. 使用传入的开始时间和结束时间计算这个期间内的日期列表。
3. 遍历日期列表,对于每个日期,遍历工作时段列表,计算这个日期内每个工作时段的工作时长(分钟),并将它们累加起来。
下面是一个参考实现:
```java
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class DeviceWorkTimeCalculator {
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static void main(String[] args) throws ParseException {
String workTimeStr = "08:00-12:00;13:00-17:00";
Date startTime = DATE_FORMAT.parse("2021-10-01 09:00:00");
Date endTime = DATE_FORMAT.parse("2021-10-03 18:00:00");
List<WorkTime> workTimes = parseWorkTime(workTimeStr);
List<Date> dates = getDatesBetween(startTime, endTime);
int totalMinutes = 0;
for (Date date : dates) {
int minutes = calculateWorkMinutesOnDate(date, workTimes);
totalMinutes += minutes;
System.out.println(DATE_FORMAT.format(date) + ": " + minutes + " minutes");
}
System.out.println("Total work minutes: " + totalMinutes);
}
private static List<WorkTime> parseWorkTime(String workTimeStr) {
List<WorkTime> workTimes = new ArrayList<>();
String[] timeSegments = workTimeStr.split(";");
for (String timeSegment : timeSegments) {
String[] parts = timeSegment.split("-");
Date startTime = parseTime(parts[0]);
Date endTime = parseTime(parts[1]);
workTimes.add(new WorkTime(startTime, endTime));
}
return workTimes;
}
private static Date parseTime(String timeStr) {
try {
return new SimpleDateFormat("HH:mm").parse(timeStr);
} catch (ParseException e) {
throw new IllegalArgumentException("Invalid time string: " + timeStr, e);
}
}
private static List<Date> getDatesBetween(Date startDate, Date endDate) {
List<Date> dates = new ArrayList<>();
long startMillis = startDate.getTime();
long endMillis = endDate.getTime();
long dayMillis = 24 * 3600 * 1000;
for (long millis = startMillis; millis <= endMillis; millis += dayMillis) {
dates.add(new Date(millis));
}
return dates;
}
private static int calculateWorkMinutesOnDate(Date date, List<WorkTime> workTimes) {
int minutes = 0;
for (WorkTime workTime : workTimes) {
Date startTime = setTime(date, workTime.getStartTime());
Date endTime = setTime(date, workTime.getEndTime());
if (endTime.before(startTime)) {
endTime = new Date(endTime.getTime() + 24 * 3600 * 1000); // 跨天
}
if (date.getTime() >= startTime.getTime() && date.getTime() < endTime.getTime()) {
minutes += (int) ((endTime.getTime() - date.getTime()) / 60000); // 毫秒转分钟
}
}
return minutes;
}
private static Date setTime(Date date, Date time) {
int hours = time.getHours();
int minutes = time.getMinutes();
int seconds = time.getSeconds();
return new Date(date.getYear(), date.getMonth(), date.getDate(), hours, minutes, seconds);
}
private static class WorkTime {
private final Date startTime;
private final Date endTime;
public WorkTime(Date startTime, Date endTime) {
this.startTime = startTime;
this.endTime = endTime;
}
public Date getStartTime() {
return startTime;
}
public Date getEndTime() {
return endTime;
}
}
}
```
这个实现假设设备的工作时段不会跨天,即每个时段的开始时间早于结束时间。如果工作时段可能跨天,可以在计算时将结束时间增加 24 小时。
阅读全文