JAVA 需求背景 有一些设备每天有固定的工作时段 和一些零散的剩余可工作时段和已用时段,需根据这些条件计算某工序是否能被设备的工作时间满足 1.传入日常工作时间段为字符串(HH:mm-HH:mm;HH:mm-HH:mm;HH:mm-HH:mm) 2.传入剩余可用工作时间段为字符串(HH:mm-HH:mm;HH:mm-HH:mm;HH:mm-HH:mm) 3.传入需要的工作时间(分) 3.判断可用工作时间段的时间是否满足需要工作时间(可用时间段的时间计算时 时间段必须对工作时间来说是连续的,中间包含休息时间也算是连续的) 4.返回true和false
时间: 2024-02-07 15:01:48 浏览: 31
可以使用 JAVA 中的日期时间类来处理这个问题,具体步骤如下:
1.将传入的日常工作时间段和剩余可用工作时间段字符串分别转化为 Date 对象的开始时间和结束时间。
2.根据开始时间和结束时间计算每个时间段的总工作时间,判断总工作时间是否能够满足需要的工作时间。
3.返回 true 或者 false。
下面是一个简单的实现示例代码:
```
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class WorkTimeCalculator {
/**
* 判断剩余可用工作时间段是否能够满足需要的工作时间
*
* @param dailyWorkTimeStr 日常工作时间段字符串
* @param remainingWorkTimeStr 剩余可用工作时间段字符串
* @param requiredWorkTime 需要的工作时间(分钟)
* @return 是否满足
*/
public boolean isWorkTimeSufficient(String dailyWorkTimeStr, String remainingWorkTimeStr, int requiredWorkTime) {
String[] dailyTimeRange = dailyWorkTimeStr.split(";");
String[] remainingTimeRange = remainingWorkTimeStr.split(";");
Date[] dailyStartTimes = new Date[dailyTimeRange.length];
Date[] dailyEndTimes = new Date[dailyTimeRange.length];
Date[] remainingStartTimes = new Date[remainingTimeRange.length];
Date[] remainingEndTimes = new Date[remainingTimeRange.length];
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
for (int i = 0; i < dailyTimeRange.length; i++) {
String[] timeRange = dailyTimeRange[i].split("-");
try {
dailyStartTimes[i] = sdf.parse(timeRange[0]);
dailyEndTimes[i] = sdf.parse(timeRange[1]);
} catch (ParseException e) {
e.printStackTrace();
}
}
for (int i = 0; i < remainingTimeRange.length; i++) {
String[] timeRange = remainingTimeRange[i].split("-");
try {
remainingStartTimes[i] = sdf.parse(timeRange[0]);
remainingEndTimes[i] = sdf.parse(timeRange[1]);
} catch (ParseException e) {
e.printStackTrace();
}
}
int availableTime = 0;
for (int i = 0; i < remainingTimeRange.length; i++) {
for (int j = 0; j < dailyTimeRange.length; j++) {
if (remainingEndTimes[i].compareTo(dailyStartTimes[j]) <= 0 || remainingStartTimes[i].compareTo(dailyEndTimes[j]) >= 0) {
// 当前剩余时间段和日常工作时间段没有交集,跳过
continue;
}
Date startTime = remainingStartTimes[i].compareTo(dailyStartTimes[j]) > 0 ? remainingStartTimes[i] : dailyStartTimes[j];
Date endTime = remainingEndTimes[i].compareTo(dailyEndTimes[j]) < 0 ? remainingEndTimes[i] : dailyEndTimes[j];
availableTime += (int) ((endTime.getTime() - startTime.getTime()) / 1000 / 60);
}
}
return availableTime >= requiredWorkTime;
}
}
```
使用示例:
```
public static void main(String[] args) {
WorkTimeCalculator calculator = new WorkTimeCalculator();
boolean isSufficient = calculator.isWorkTimeSufficient("08:00-12:00;13:00-17:00", "09:00-11:00;14:00-16:00", 120);
System.out.println(isSufficient);
}
```
输出结果为 true,表示剩余可用工作时间段能够满足 120 分钟的工作时间要求。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)