JAVA 计算指定工作日的工时 单位(分钟) 1.传入工作时段字符串,格式为String(HH:mm-HH:mm;HH:mm-HH:mm;HH:mm-HH:mm), 2.传入工作起止日期时间(Date类型) 3.传入一个指定年月日(Date类型) 4.计算第三步中指定的这天工作日共工作了多少(分钟)
时间: 2024-01-23 17:02:36 浏览: 183
以下是一个计算指定工作日工时的 Java 代码示例:
```java
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class WorkHourCalculator {
public static void main(String[] args) throws Exception {
String workingHours = "09:00-12:00;13:00-18:00"; // 工作时间段
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date startDate = dateFormat.parse("2021-03-01 09:00:00"); // 工作起始时间
Date endDate = dateFormat.parse("2021-03-31 18:00:00"); // 工作结束时间
Date targetDate = dateFormat.parse("2021-03-15 12:00:00"); // 指定日期
long workMinutes = calculateWorkMinutes(workingHours, startDate, endDate, targetDate);
System.out.println("工作日工时:" + workMinutes + " 分钟");
}
private static long calculateWorkMinutes(String workingHours, Date startDate, Date endDate, Date targetDate) throws Exception {
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
Calendar startCalendar = Calendar.getInstance();
startCalendar.setTime(startDate);
Calendar endCalendar = Calendar.getInstance();
endCalendar.setTime(endDate);
long workMinutes = 0;
if (!startCalendar.after(endCalendar)) { // 判断起始时间是否在结束时间之前
Calendar targetCalendar = Calendar.getInstance();
targetCalendar.setTime(targetDate);
targetCalendar.set(Calendar.HOUR_OF_DAY, 0);
targetCalendar.set(Calendar.MINUTE, 0);
targetCalendar.set(Calendar.SECOND, 0);
targetCalendar.set(Calendar.MILLISECOND, 0);
while (!startCalendar.after(endCalendar)) { // 遍历起始时间和结束时间之间的每一天
if (isWorkDay(workingHours, startCalendar)) { // 判断当前日期是否为工作日
if (targetCalendar.equals(startCalendar)) { // 判断当前日期是否为指定日期
workMinutes += calculateWorkMinutesForDay(workingHours, startCalendar, targetDate);
break;
} else {
workMinutes += calculateWorkMinutesForDay(workingHours, startCalendar, null);
}
}
startCalendar.add(Calendar.DAY_OF_YEAR, 1);
}
}
return workMinutes;
}
private static boolean isWorkDay(String workingHours, Calendar calendar) throws Exception {
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
String[] workingHoursArray = workingHours.split(";");
for (String workingHoursItem : workingHoursArray) {
String[] workingHoursItemArray = workingHoursItem.split("-");
Date startTime = dateFormat.parse(workingHoursItemArray[0]);
Date endTime = dateFormat.parse(workingHoursItemArray[1]);
Calendar startCalendar = Calendar.getInstance();
startCalendar.setTime(startTime);
Calendar endCalendar = Calendar.getInstance();
endCalendar.setTime(endTime);
if (calendar.get(Calendar.HOUR_OF_DAY) >= startCalendar.get(Calendar.HOUR_OF_DAY) &&
calendar.get(Calendar.MINUTE) >= startCalendar.get(Calendar.MINUTE) &&
calendar.get(Calendar.HOUR_OF_DAY) < endCalendar.get(Calendar.HOUR_OF_DAY) ||
(calendar.get(Calendar.HOUR_OF_DAY) == endCalendar.get(Calendar.HOUR_OF_DAY) &&
calendar.get(Calendar.MINUTE) <= endCalendar.get(Calendar.MINUTE))) {
return true;
}
}
return false;
}
private static long calculateWorkMinutesForDay(String workingHours, Calendar calendar, Date targetDate) throws Exception {
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
String[] workingHoursArray = workingHours.split(";");
long workMinutes = 0;
for (String workingHoursItem : workingHoursArray) {
String[] workingHoursItemArray = workingHoursItem.split("-");
Date startTime = dateFormat.parse(workingHoursItemArray[0]);
Date endTime = dateFormat.parse(workingHoursItemArray[1]);
Calendar startCalendar = Calendar.getInstance();
startCalendar.setTime(startTime);
Calendar endCalendar = Calendar.getInstance();
endCalendar.setTime(endTime);
if (targetDate != null && calendar.equals(startCalendar)) { // 如果当前日期是指定日期,则计算当日剩余工时
workMinutes += calculateWorkMinutesBetweenTimes(calendar.getTime(), endTime);
} else if (targetDate != null && calendar.equals(endCalendar)) { // 如果当前日期是指定日期,则计算当日已完成工时
workMinutes += calculateWorkMinutesBetweenTimes(startTime, calendar.getTime());
break;
} else { // 如果当前日期不是指定日期,则计算当日全部工时
workMinutes += calculateWorkMinutesBetweenTimes(startTime, endTime);
}
}
return workMinutes;
}
private static long calculateWorkMinutesBetweenTimes(Date startTime, Date endTime) {
long startMillis = startTime.getTime();
long endMillis = endTime.getTime();
long interval = endMillis - startMillis;
return interval / (1000 * 60);
}
}
```
在上述代码中,`calculateWorkMinutes` 方法用于计算指定日期的工作分钟数。该方法首先遍历起始时间和结束时间之间的每一天,然后判断每一天是否为工作日。如果当前日期是指定日期,则计算当日剩余工时或已完成工时;否则计算当日全部工时。最终将所有工作日的工时相加得到总工时。`isWorkDay` 方法用于判断当前日期是否为工作日,`calculateWorkMinutesForDay` 方法用于计算指定日期的工作分钟数。
需要注意的是,上述代码中的时间字符串格式为 "HH:mm",日期时间字符串格式为 "yyyy-MM-dd HH:mm:ss",可以根据实际情况进行调整。此外,上述代码还做了一些异常处理,需要在方法调用中进行异常捕获或抛出。
阅读全文