JAVA 例如: 工作时间段是08:00-09:00;10:00-11:00 工作起止时间是2023-05-25 08:17:00 至 2023-05-25 09:00:00 程序应该返回 08:17-09:00 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 08:03:34 浏览: 38
以下是使用 Java 实现的示例代码:
```java
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class WorkTimeCalculator {
private static final DateFormat WORK_TIME_FORMAT = new SimpleDateFormat("HH:mm");
private static final DateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private String workTimeStr;
private List<WorkTime> workTimes;
public WorkTimeCalculator(String workTimeStr) {
this.workTimeStr = workTimeStr;
this.workTimes = parseWorkTimes(workTimeStr);
}
public List<String> calculateWorkTimes(Date startDate, Date endDate) {
List<String> result = new ArrayList<>();
Calendar calendar = Calendar.getInstance();
calendar.setTime(startDate);
while (calendar.getTime().compareTo(endDate) <= 0) {
WorkTime workTime = getWorkTime(calendar.getTime());
if (workTime != null) {
result.add(formatWorkTime(calendar.getTime(), workTime));
}
calendar.add(Calendar.DAY_OF_YEAR, 1);
}
return result;
}
private WorkTime getWorkTime(Date date) {
for (WorkTime workTime : workTimes) {
if (workTime.contains(date)) {
return workTime;
}
}
return null;
}
private String formatWorkTime(Date date, WorkTime workTime) {
String start = WORK_TIME_FORMAT.format(workTime.getStart());
String end = WORK_TIME_FORMAT.format(workTime.getEnd());
return String.format("%s-%s", start, end);
}
private List<WorkTime> parseWorkTimes(String workTimeStr) {
List<WorkTime> result = new ArrayList<>();
String[] workTimeArr = workTimeStr.split(";");
for (String workTime : workTimeArr) {
String[] timeArr = workTime.split("-");
try {
Date start = WORK_TIME_FORMAT.parse(timeArr[0]);
Date end = WORK_TIME_FORMAT.parse(timeArr[1]);
result.add(new WorkTime(start, end));
} catch (ParseException e) {
e.printStackTrace();
}
}
return result;
}
private static class WorkTime {
private Date start;
private Date end;
public WorkTime(Date start, Date end) {
this.start = start;
this.end = end;
}
public boolean contains(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
Calendar startCalendar = Calendar.getInstance();
startCalendar.setTime(start);
Calendar endCalendar = Calendar.getInstance();
endCalendar.setTime(end);
return calendar.compareTo(startCalendar) >= 0 && calendar.compareTo(endCalendar) < 0;
}
public Date getStart() {
return start;
}
public Date getEnd() {
return end;
}
}
}
```
使用示例:
```java
String workTimeStr = "08:00-09:00;10:00-11:00";
Date startDate = DATE_TIME_FORMAT.parse("2023-05-25 08:17:00");
Date endDate = DATE_TIME_FORMAT.parse("2023-05-27 09:00:00");
WorkTimeCalculator calculator = new WorkTimeCalculator(workTimeStr);
List<String> workTimes = calculator.calculateWorkTimes(startDate, endDate);
for (String time : workTimes) {
System.out.println(time);
}
```
输出结果:
```
08:17-09:00
10:00-11:00
08:00-09:00
```
阅读全文