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) 5.计算设备指定天工作了多少分钟 单位(分钟)
时间: 2024-02-05 10:12:42 浏览: 81
以下是 JAVA 实现代码:
```java
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DeviceWorkTimeCalculator {
/**
* 计算设备在指定日期范围内的工作时间(分钟)
* @param workTimeStr 设备工作时段字符串(格式为:HH:mm-HH:mm;HH:mm-HH:mm;HH:mm-HH:mm)
* @param startDate 设备工作开始日期时间
* @param endDate 设备工作结束日期时间
* @return 设备工作时间(分钟)
*/
public static int calculateDeviceWorkTime(String workTimeStr, Date startDate, Date endDate) {
// 将设备工作时段字符串解析为日历对象数组
Calendar[] workTimeCalendars = parseWorkTimeStr(workTimeStr);
if (workTimeCalendars == null || workTimeCalendars.length == 0) {
return 0;
}
// 将设备工作开始日期时间和结束日期时间转换为日历对象
Calendar startCalendar = Calendar.getInstance();
startCalendar.setTime(startDate);
Calendar endCalendar = Calendar.getInstance();
endCalendar.setTime(endDate);
// 如果设备工作开始时间在设备工作结束时间之后,则返回 0
if (startCalendar.after(endCalendar)) {
return 0;
}
int totalWorkTime = 0;
// 循环遍历设备工作时段日历对象数组
for (Calendar workTimeCalendar : workTimeCalendars) {
// 得到当天的工作开始时间和结束时间
Calendar startTime = getStartTime(startCalendar, workTimeCalendar);
Calendar endTime = getEndTime(endCalendar, workTimeCalendar);
// 如果开始时间在结束时间之后,则说明当天不工作,跳过计算
if (startTime.after(endTime)) {
continue;
}
// 计算当天的工作时间,并累加到总工作时间上
int workTime = calculateWorkTime(startTime, endTime);
totalWorkTime += workTime;
}
return totalWorkTime;
}
/**
* 将设备工作时段字符串解析为日历对象数组
* @param workTimeStr 设备工作时段字符串(格式为:HH:mm-HH:mm;HH:mm-HH:mm;HH:mm-HH:mm)
* @return 解析后的日历对象数组
*/
private static Calendar[] parseWorkTimeStr(String workTimeStr) {
String[] workTimeArr = workTimeStr.split(";");
Calendar[] workTimeCalendars = new Calendar[workTimeArr.length];
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
for (int i = 0; i < workTimeArr.length; i++) {
String[] timeArr = workTimeArr[i].split("-");
try {
Calendar startCalendar = Calendar.getInstance();
startCalendar.setTime(sdf.parse(timeArr[0]));
Calendar endCalendar = Calendar.getInstance();
endCalendar.setTime(sdf.parse(timeArr[1]));
workTimeCalendars[i] = startCalendar;
workTimeCalendars[i].set(Calendar.HOUR_OF_DAY, startCalendar.get(Calendar.HOUR_OF_DAY));
workTimeCalendars[i].set(Calendar.MINUTE, startCalendar.get(Calendar.MINUTE));
workTimeCalendars[i].set(Calendar.SECOND, 0);
workTimeCalendars[i].set(Calendar.MILLISECOND, 0);
if (endCalendar.before(startCalendar)) {
endCalendar.add(Calendar.DAY_OF_MONTH, 1);
}
while (workTimeCalendars[i].before(endCalendar)) {
workTimeCalendars[i].add(Calendar.DAY_OF_MONTH, 1);
}
} catch (Exception e) {
e.printStackTrace();
}
}
return workTimeCalendars;
}
/**
* 得到当天的工作开始时间
* @param startCalendar 设备工作开始日期时间
* @param workTimeCalendar 当天的工作时段日历对象
* @return 当天的工作开始时间
*/
private static Calendar getStartTime(Calendar startCalendar, Calendar workTimeCalendar) {
Calendar startTime = Calendar.getInstance();
startTime.set(Calendar.YEAR, workTimeCalendar.get(Calendar.YEAR));
startTime.set(Calendar.MONTH, workTimeCalendar.get(Calendar.MONTH));
startTime.set(Calendar.DAY_OF_MONTH, workTimeCalendar.get(Calendar.DAY_OF_MONTH));
startTime.set(Calendar.HOUR_OF_DAY, workTimeCalendar.get(Calendar.HOUR_OF_DAY));
startTime.set(Calendar.MINUTE, workTimeCalendar.get(Calendar.MINUTE));
startTime.set(Calendar.SECOND, 0);
startTime.set(Calendar.MILLISECOND, 0);
if (startTime.before(startCalendar)) {
startTime.setTime(startCalendar.getTime());
}
return startTime;
}
/**
* 得到当天的工作结束时间
* @param endCalendar 设备工作结束日期时间
* @param workTimeCalendar 当天的工作时段日历对象
* @return 当天的工作结束时间
*/
private static Calendar getEndTime(Calendar endCalendar, Calendar workTimeCalendar) {
Calendar endTime = Calendar.getInstance();
endTime.set(Calendar.YEAR, workTimeCalendar.get(Calendar.YEAR));
endTime.set(Calendar.MONTH, workTimeCalendar.get(Calendar.MONTH));
endTime.set(Calendar.DAY_OF_MONTH, workTimeCalendar.get(Calendar.DAY_OF_MONTH));
endTime.set(Calendar.HOUR_OF_DAY, workTimeCalendar.get(Calendar.HOUR_OF_DAY));
endTime.set(Calendar.MINUTE, workTimeCalendar.get(Calendar.MINUTE));
endTime.set(Calendar.SECOND, 0);
endTime.set(Calendar.MILLISECOND, 0);
if (endTime.after(endCalendar)) {
endTime.setTime(endCalendar.getTime());
}
return endTime;
}
/**
* 计算工作时间(分钟)
* @param startTime 工作开始时间
* @param endTime 工作结束时间
* @return 工作时间(分钟)
*/
private static int calculateWorkTime(Calendar startTime, Calendar endTime) {
long workTimeMillis = endTime.getTimeInMillis() - startTime.getTimeInMillis();
int workTimeMinutes = (int) (workTimeMillis / (1000 * 60));
return workTimeMinutes;
}
}
```
使用方法如下:
```java
String workTimeStr = "08:30-12:00;13:30-17:30";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date startDate = sdf.parse("2021-10-01 09:00:00");
Date endDate = sdf.parse("2021-10-03 18:00:00");
int workTime = DeviceWorkTimeCalculator.calculateDeviceWorkTime(workTimeStr, startDate, endDate);
System.out.println("设备工作时间(分钟):" + workTime);
```
其中,`workTimeStr` 表示设备的工作时段,`startDate` 和 `endDate` 分别表示设备的工作开始日期时间和结束日期时间,`workTime` 表示设备在指定日期范围内的工作时间(分钟)。
阅读全文