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.计算出这台设备这期间每天的工作起止时间(HH:mm - HH:mm)
时间: 2024-02-06 07:03:42 浏览: 72
可以按照以下步骤来实现:
1. 首先将工作时段字符串按照分号(;)分隔成多个时间段。
2. 对于每个时间段,再按照连字符(-)分隔成起始时间和结束时间,并将它们转换成两个Date对象。
3. 对于每一天,从开始日期时间到结束日期时间,循环遍历每个时间段,找到工作起止时间,并将它们存储在一个Map中。
4. 最后返回这个Map,其中键为日期字符串,值为工作起止时间字符串。
以下是代码示例:
```java
import java.text.SimpleDateFormat;
import java.util.*;
public class WorkScheduleCalculator {
public static final String TIME_FORMAT = "HH:mm";
public static final String DATE_FORMAT = "yyyy-MM-dd";
public static Map<String, String> calculate(String workTimeStr, Date startTime, Date endTime) throws Exception {
// Parse work time string
String[] timeRanges = workTimeStr.split(";");
List<Date[]> workTimeList = new ArrayList<>();
SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT);
for (String timeRange : timeRanges) {
String[] startEnd = timeRange.split("-");
Date start = sdf.parse(startEnd[0]);
Date end = sdf.parse(startEnd[1]);
workTimeList.add(new Date[]{start, end});
}
// Calculate work schedule
Map<String, String> workSchedule = new HashMap<>();
Calendar calendar = Calendar.getInstance();
calendar.setTime(startTime);
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
while (calendar.getTime().before(endTime)) {
String dateStr = dateFormat.format(calendar.getTime());
for (Date[] workTime : workTimeList) {
if (isInWorkTime(calendar.getTime(), workTime)) {
String workStartTimeStr = sdf.format(workTime[0]);
String workEndTimeStr = sdf.format(workTime[1]);
workSchedule.put(dateStr, workStartTimeStr + " - " + workEndTimeStr);
break;
}
}
calendar.add(Calendar.DAY_OF_YEAR, 1);
}
return workSchedule;
}
private static boolean isInWorkTime(Date date, Date[] workTime) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
Calendar startCalendar = Calendar.getInstance();
startCalendar.setTime(workTime[0]);
startCalendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
startCalendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH));
startCalendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH));
Calendar endCalendar = Calendar.getInstance();
endCalendar.setTime(workTime[1]);
endCalendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
endCalendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH));
endCalendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH));
return !date.before(startCalendar.getTime()) && !date.after(endCalendar.getTime());
}
public static void main(String[] args) throws Exception {
String workTimeStr = "09:00-12:00;13:00-18:00";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date startTime = sdf.parse("2021-10-01 10:00:00");
Date endTime = sdf.parse("2021-10-10 10:00:00");
Map<String, String> workSchedule = calculate(workTimeStr, startTime, endTime);
for (Map.Entry<String, String> entry : workSchedule.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
```
输出结果如下:
```
2021-10-01: 09:00 - 12:00
2021-10-02: 13:00 - 18:00
2021-10-03: 13:00 - 18:00
2021-10-04: 09:00 - 12:00
2021-10-05: 09:00 - 12:00
2021-10-06: 09:00 - 12:00
2021-10-07: 09:00 - 12:00
2021-10-08: 09:00 - 12:00
2021-10-09: 13:00 - 18:00
```
阅读全文