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:29 浏览: 192
可以按照以下步骤实现:
1. 解析工作时段字符串,得到每个时间段的起止时间,存储为类似于以下格式的对象列表:
```
List<TimeRange> timeRanges = new ArrayList<>();
timeRanges.add(new TimeRange(startTime1, endTime1));
timeRanges.add(new TimeRange(startTime2, endTime2));
// ...
```
其中 `TimeRange` 是一个自定义类,包含起止时间的 `Date` 对象。
2. 将工作开始日期时间和工作结束日期时间转化为 `Calendar` 对象,以便计算每天的起止时间。可以使用以下代码:
```
Calendar startCal = Calendar.getInstance();
startCal.setTime(startDate);
Calendar endCal = Calendar.getInstance();
endCal.setTime(endDate);
```
3. 循环遍历每一天,计算该天的工作起止时间。对于每一天,首先获取该天的起始时间和结束时间:
```
Calendar dayStart = (Calendar) startCal.clone();
dayStart.set(Calendar.HOUR_OF_DAY, 0);
dayStart.set(Calendar.MINUTE, 0);
dayStart.set(Calendar.SECOND, 0);
dayStart.set(Calendar.MILLISECOND, 0);
Calendar dayEnd = (Calendar) endCal.clone();
dayEnd.set(Calendar.HOUR_OF_DAY, 23);
dayEnd.set(Calendar.MINUTE, 59);
dayEnd.set(Calendar.SECOND, 59);
dayEnd.set(Calendar.MILLISECOND, 999);
```
4. 遍历每个工作时段,判断该时段是否在该天内。如果是,则计算该时段在该天内的起止时间,并将其加入起止时间列表:
```
List<TimeRange> dayTimeRanges = new ArrayList<>();
for (TimeRange timeRange : timeRanges) {
if (timeRange.getStart().before(dayEnd.getTime()) &&
timeRange.getEnd().after(dayStart.getTime())) {
Date startTime = (timeRange.getStart().after(dayStart.getTime())) ? timeRange.getStart() : dayStart.getTime();
Date endTime = (timeRange.getEnd().before(dayEnd.getTime())) ? timeRange.getEnd() : dayEnd.getTime();
dayTimeRanges.add(new TimeRange(startTime, endTime));
}
}
```
5. 最后,将每天的起止时间列表格式化为字符串(HH:mm - HH:mm),并存储到一个 `Map` 中,以日期作为键:
```
Map<String, String> workTimesByDay = new HashMap<>();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
for (Calendar cal = (Calendar) startCal.clone(); cal.before(endCal); cal.add(Calendar.DATE, 1)) {
String dayStr = dateFormat.format(cal.getTime());
List<String> timeRangeStrs = new ArrayList<>();
for (TimeRange timeRange : dayTimeRanges) {
if (dateFormat.format(timeRange.getStart()).equals(dayStr)) {
timeRangeStrs.add(String.format("%tR - %tR", timeRange.getStart(), timeRange.getEnd()));
}
}
workTimesByDay.put(dayStr, String.join("; ", timeRangeStrs));
}
```
完整代码如下:
```
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class WorkTimeCalculator {
public static void main(String[] args) throws Exception {
String timeRangeStr = "08:00-12:00;13:00-17:00";
Date startDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2021-01-01 00:00:00");
Date endDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2021-01-10 23:59:59");
List<TimeRange> timeRanges = parseTimeRanges(timeRangeStr);
Map<String, String> workTimesByDay = calculateWorkTimesByDay(timeRanges, startDate, endDate);
for (Map.Entry<String, String> entry : workTimesByDay.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
private static List<TimeRange> parseTimeRanges(String timeRangeStr) {
List<TimeRange> timeRanges = new ArrayList<>();
String[] timeRangeParts = timeRangeStr.split(";");
for (String timeRangePart : timeRangeParts) {
String[] timeParts = timeRangePart.split("-");
Date startTime = parseTime(timeParts[0]);
Date endTime = parseTime(timeParts[1]);
timeRanges.add(new TimeRange(startTime, endTime));
}
return timeRanges;
}
private static Date parseTime(String timeStr) {
try {
return new SimpleDateFormat("HH:mm").parse(timeStr);
} catch (Exception e) {
throw new RuntimeException("Invalid time format: " + timeStr);
}
}
private static Map<String, String> calculateWorkTimesByDay(List<TimeRange> timeRanges, Date startDate, Date endDate) {
Map<String, String> workTimesByDay = new HashMap<>();
Calendar startCal = Calendar.getInstance();
startCal.setTime(startDate);
Calendar endCal = Calendar.getInstance();
endCal.setTime(endDate);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
for (Calendar cal = (Calendar) startCal.clone(); cal.before(endCal); cal.add(Calendar.DATE, 1)) {
String dayStr = dateFormat.format(cal.getTime());
Calendar dayStart = (Calendar) cal.clone();
dayStart.set(Calendar.HOUR_OF_DAY, 0);
dayStart.set(Calendar.MINUTE, 0);
dayStart.set(Calendar.SECOND, 0);
dayStart.set(Calendar.MILLISECOND, 0);
Calendar dayEnd = (Calendar) cal.clone();
dayEnd.set(Calendar.HOUR_OF_DAY, 23);
dayEnd.set(Calendar.MINUTE, 59);
dayEnd.set(Calendar.SECOND, 59);
dayEnd.set(Calendar.MILLISECOND, 999);
List<TimeRange> dayTimeRanges = new ArrayList<>();
for (TimeRange timeRange : timeRanges) {
if (timeRange.getStart().before(dayEnd.getTime()) &&
timeRange.getEnd().after(dayStart.getTime())) {
Date startTime = (timeRange.getStart().after(dayStart.getTime())) ? timeRange.getStart() : dayStart.getTime();
Date endTime = (timeRange.getEnd().before(dayEnd.getTime())) ? timeRange.getEnd() : dayEnd.getTime();
dayTimeRanges.add(new TimeRange(startTime, endTime));
}
}
List<String> timeRangeStrs = new ArrayList<>();
for (TimeRange timeRange : dayTimeRanges) {
if (dateFormat.format(timeRange.getStart()).equals(dayStr)) {
timeRangeStrs.add(String.format("%tR - %tR", timeRange.getStart(), timeRange.getEnd()));
}
}
workTimesByDay.put(dayStr, String.join("; ", timeRangeStrs));
}
return workTimesByDay;
}
private static class TimeRange {
private Date start;
private Date end;
public TimeRange(Date start, Date end) {
this.start = start;
this.end = end;
}
public Date getStart() {
return start;
}
public Date getEnd() {
return end;
}
}
}
```
阅读全文