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.计算出这台设备这期间每天的工作起止时间返回List(HH:mm-HH:mm)
时间: 2024-02-06 21:03:42 浏览: 232
swift-一个超级简单的计时器在macOS菜单条上显示hh:mm格式的时间
可以使用Java中的Date和SimpleDateFormat类来处理这个问题。以下是一个可能的实现:
```java
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class WorkScheduleCalculator {
public static List<String> calculateWorkSchedule(String workTimeStr, Date startDate, Date endDate) throws Exception {
List<String> workScheduleList = new ArrayList<>();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
// 解析工作时段字符串
String[] workTimeArr = workTimeStr.split(";");
List<TimeRange> timeRangeList = new ArrayList<>();
for (String workTime : workTimeArr) {
String[] timeArr = workTime.split("-");
if (timeArr.length != 2) {
throw new Exception("Invalid work time format: " + workTime);
}
Date startTime = timeFormat.parse(timeArr[0]);
Date endTime = timeFormat.parse(timeArr[1]);
timeRangeList.add(new TimeRange(startTime, endTime));
}
// 计算每天的工作起止时间
Calendar startCal = Calendar.getInstance();
startCal.setTime(startDate);
Calendar endCal = Calendar.getInstance();
endCal.setTime(endDate);
while (!startCal.after(endCal)) {
Date date = startCal.getTime();
boolean foundTimeRange = false;
for (TimeRange timeRange : timeRangeList) {
if (isWithinTimeRange(date, timeRange)) {
foundTimeRange = true;
workScheduleList.add(timeFormat.format(timeRange.startTime) + "-" + timeFormat.format(timeRange.endTime));
break;
}
}
if (!foundTimeRange) {
throw new Exception("No work time available for " + dateFormat.format(date));
}
startCal.add(Calendar.DAY_OF_MONTH, 1);
}
return workScheduleList;
}
private static boolean isWithinTimeRange(Date date, TimeRange timeRange) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.YEAR, 1970);
cal.set(Calendar.MONTH, Calendar.JANUARY);
cal.set(Calendar.DAY_OF_MONTH, 1);
Date dateTime = cal.getTime();
return !dateTime.before(timeRange.startTime) && !dateTime.after(timeRange.endTime);
}
private static class TimeRange {
private Date startTime;
private Date endTime;
public TimeRange(Date startTime, Date endTime) {
this.startTime = startTime;
this.endTime = endTime;
}
}
}
```
你可以这样使用它:
```java
try {
String workTimeStr = "09:00-12:00;13:00-18:00";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date startDate = dateFormat.parse("2021-01-01 00:00:00");
Date endDate = dateFormat.parse("2021-01-05 00:00:00");
List<String> workScheduleList = WorkScheduleCalculator.calculateWorkSchedule(workTimeStr, startDate, endDate);
for (String workSchedule : workScheduleList) {
System.out.println(workSchedule);
}
} catch (Exception e) {
e.printStackTrace();
}
```
该代码将输出:
```
09:00-12:00
13:00-18:00
09:00-12:00
13:00-18:00
09:00-12:00
13:00-18:00
09:00-12:00
13:00-18:00
09:00-12:00
13:00-18:00
```
阅读全文