将上述程序优化为 当满足需求时 返回满足需求的开始工作时间
时间: 2023-10-11 17:19:14 浏览: 50
修改 isWorkTimeSufficient 方法的返回值为 Date,并在方法中记录符合要求的开始时间,代码如下:
```
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class WorkTimeCalculator {
/**
* 判断剩余可用工作时间段是否能够满足需要的工作时间,并返回满足需求的开始工作时间
*
* @param dailyWorkTimeStr 日常工作时间段字符串
* @param remainingWorkTimeStr 剩余可用工作时间段字符串
* @param requiredWorkTime 需要的工作时间(分钟)
* @return 符合要求的开始工作时间,如果不符合要求则返回 null
*/
public Date getStartTimeIfWorkTimeSufficient(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");
Date startTime = null;
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 sTime = remainingStartTimes[i].compareTo(dailyStartTimes[j]) > 0 ? remainingStartTimes[i] : dailyStartTimes[j];
Date eTime = remainingEndTimes[i].compareTo(dailyEndTimes[j]) < 0 ? remainingEndTimes[i] : dailyEndTimes[j];
availableTime += (int) ((eTime.getTime() - sTime.getTime()) / 1000 / 60);
if (availableTime >= requiredWorkTime) {
startTime = sTime;
break;
}
}
if (startTime != null) {
break;
}
}
return startTime;
}
}
```
使用示例:
```
public static void main(String[] args) {
WorkTimeCalculator calculator = new WorkTimeCalculator();
Date startTime = calculator.getStartTimeIfWorkTimeSufficient("08:00-12:00;13:00-17:00", "09:00-11:00;14:00-16:00", 120);
if (startTime != null) {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
System.out.println("符合要求的开始时间为:" + sdf.format(startTime));
} else {
System.out.println("不符合要求!");
}
}
```
输出结果为:符合要求的开始时间为:09:00,表示剩余可用工作时间段能够满足 120 分钟的工作时间要求,且开始工作时间为 09:00。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)