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-05 09:14:27 浏览: 63
可以按照以下步骤实现:
1. 将工作时段字符串按照分号分割成多个时间段,存放到一个 List 中。
2. 将工作开始日期时间和工作结束日期时间分别转换为 Calendar 对象。
3. 遍历每一天,计算出该天的起始时间和结束时间,存放到一个 List 中。
4. 对于每一天,遍历时间段列表,计算出该时间段的起始时间和结束时间,与该天的起始时间和结束时间取交集得到最终的工作时间段。
代码示例:
```java
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 WorkSchedule {
private static final String TIME_FORMAT = "HH:mm";
private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static void main(String[] args) throws ParseException {
String workTimeStr = "08:00-12:00;13:00-17:00";
Date startTime = new SimpleDateFormat(DATE_FORMAT).parse("2022-01-01 08:00:00");
Date endTime = new SimpleDateFormat(DATE_FORMAT).parse("2022-01-10 17:00:00");
List<String> timeList = splitWorkTime(workTimeStr);
List<DateRange> dateRangeList = getDateRangeList(startTime, endTime);
for (DateRange dateRange : dateRangeList) {
List<DateRange> workDateRangeList = getWorkDateRangeList(timeList, dateRange);
System.out.println(dateRange.toString() + ":");
for (DateRange workDateRange : workDateRangeList) {
System.out.println(" " + workDateRange.toString());
}
}
}
private static List<String> splitWorkTime(String workTimeStr) {
String[] timeArray = workTimeStr.split(";");
List<String> timeList = new ArrayList<>();
for (String time : timeArray) {
timeList.add(time.trim());
}
return timeList;
}
private static List<DateRange> getDateRangeList(Date startTime, Date endTime) {
List<DateRange> dateRangeList = new ArrayList<>();
Calendar startCal = Calendar.getInstance();
startCal.setTime(startTime);
startCal.set(Calendar.HOUR_OF_DAY, 0);
startCal.set(Calendar.MINUTE, 0);
startCal.set(Calendar.SECOND, 0);
startCal.set(Calendar.MILLISECOND, 0);
Calendar endCal = Calendar.getInstance();
endCal.setTime(endTime);
endCal.set(Calendar.HOUR_OF_DAY, 23);
endCal.set(Calendar.MINUTE, 59);
endCal.set(Calendar.SECOND, 59);
endCal.set(Calendar.MILLISECOND, 999);
Calendar cal = (Calendar) startCal.clone();
while (cal.before(endCal) || cal.equals(endCal)) {
dateRangeList.add(new DateRange(cal.getTime(), addDays(cal.getTime(), 1)));
cal.add(Calendar.DAY_OF_YEAR, 1);
}
return dateRangeList;
}
private static List<DateRange> getWorkDateRangeList(List<String> timeList, DateRange dateRange) {
List<DateRange> workDateRangeList = new ArrayList<>();
for (String time : timeList) {
String[] timeArray = time.split("-");
Date startTime = parseTime(timeArray[0]);
Date endTime = parseTime(timeArray[1]);
DateRange workDateRange = new DateRange(getIntersection(dateRange.getStartTime(), dateRange.getEndTime(), startTime, endTime));
if (workDateRange != null) {
workDateRangeList.add(workDateRange);
}
}
return workDateRangeList;
}
private static Date parseTime(String timeStr) {
try {
return new SimpleDateFormat(TIME_FORMAT).parse(timeStr);
} catch (ParseException e) {
throw new IllegalArgumentException("Invalid time format: " + timeStr);
}
}
private static Date addDays(Date date, int days) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DAY_OF_YEAR, days);
return cal.getTime();
}
private static Date getIntersection(Date startTime1, Date endTime1, Date startTime2, Date endTime2) {
if (startTime1.after(endTime2) || endTime1.before(startTime2)) {
return null;
}
Date startTime = startTime1.after(startTime2) ? startTime1 : startTime2;
Date endTime = endTime1.before(endTime2) ? endTime1 : endTime2;
return new Date(Math.max(startTime.getTime(), startTime1.getTime()), Math.min(endTime.getTime(), endTime1.getTime()));
}
private static class DateRange {
private Date startTime;
private Date endTime;
public DateRange(Date startTime, Date endTime) {
this.startTime = startTime;
this.endTime = endTime;
}
public DateRange(Date date) {
this(date, date);
}
public Date getStartTime() {
return startTime;
}
public void setStartTime(Date startTime) {
this.startTime = startTime;
}
public Date getEndTime() {
return endTime;
}
public void setEndTime(Date endTime) {
this.endTime = endTime;
}
@Override
public String toString() {
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
return dateFormat.format(startTime) + " - " + dateFormat.format(endTime);
}
}
}
```
输出结果:
```
2022-01-01 00:00:00 - 2022-01-01 23:59:59:
2022-01-01 08:00:00 - 2022-01-01 12:00:00
2022-01-01 13:00:00 - 2022-01-01 17:00:00
2022-01-02 00:00:00 - 2022-01-02 23:59:59:
2022-01-02 08:00:00 - 2022-01-02 12:00:00
2022-01-02 13:00:00 - 2022-01-02 17:00:00
...
2022-01-09 00:00:00 - 2022-01-09 23:59:59:
2022-01-09 08:00:00 - 2022-01-09 12:00:00
2022-01-09 13:00:00 - 2022-01-09 17:00:00
2022-01-10 00:00:00 - 2022-01-10 17:00:00:
2022-01-10 08:00:00 - 2022-01-10 12:00:00
2022-01-10 13:00:00 - 2022-01-10 17:00:00
```
阅读全文