JAVA 计算指定工作日工作了多少分钟 单位(分钟) 1.传入工作时段字符串,格式为String(HH:mm-HH:mm;HH:mm-HH:mm;HH:mm-HH:mm), 2.传入工作开始日期时间(Date类型) 3.传入工作结束日期时间(Date类型) 4.传入一个指定年月日(Date类型),该日期已知在工作开始和结束日期之间
时间: 2024-01-23 14:02:36 浏览: 82
工作日计算的JAVA实现
5星 · 资源好评率100%
以下是 Java 代码实现,注释中有详细说明:
```
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class WorkMinutesCalculator {
/**
* 计算指定工作日工作了多少分钟
*
* @param workTimeStr 工作时间段字符串,格式为String(HH:mm-HH:mm;HH:mm-HH:mm;HH:mm-HH:mm)
* @param workStartTime 工作开始日期时间(Date类型)
* @param workEndTime 工作结束日期时间(Date类型)
* @param specifiedDate 指定年月日(Date类型),该日期已知在工作开始和结束日期之间
* @return 指定工作日工作的分钟数
*/
public static int calculateWorkMinutes(String workTimeStr, Date workStartTime, Date workEndTime, Date specifiedDate) {
// 将工作时间段字符串解析成时间段数组
String[] workTimeArr = workTimeStr.split(";");
// 计算指定日期是工作日还是休息日
boolean isWorkday = isWorkday(specifiedDate, workTimeArr);
if (!isWorkday) {
// 如果指定日期是休息日,则返回0
return 0;
}
// 获取指定日期的工作开始时间和工作结束时间
Calendar specifiedCalendar = Calendar.getInstance();
specifiedCalendar.setTime(specifiedDate);
Calendar workStartCalendar = Calendar.getInstance();
workStartCalendar.setTime(workStartTime);
workStartCalendar.set(Calendar.YEAR, specifiedCalendar.get(Calendar.YEAR));
workStartCalendar.set(Calendar.MONTH, specifiedCalendar.get(Calendar.MONTH));
workStartCalendar.set(Calendar.DAY_OF_MONTH, specifiedCalendar.get(Calendar.DAY_OF_MONTH));
Calendar workEndCalendar = Calendar.getInstance();
workEndCalendar.setTime(workEndTime);
workEndCalendar.set(Calendar.YEAR, specifiedCalendar.get(Calendar.YEAR));
workEndCalendar.set(Calendar.MONTH, specifiedCalendar.get(Calendar.MONTH));
workEndCalendar.set(Calendar.DAY_OF_MONTH, specifiedCalendar.get(Calendar.DAY_OF_MONTH));
// 计算指定日期的工作时长
int workMinutes = calculateWorkMinutes(workTimeArr, workStartCalendar, workEndCalendar);
return workMinutes;
}
/**
* 判断指定日期是否为工作日
*
* @param date 指定日期
* @param workTimeArr 工作时间段数组
* @return true:是工作日,false:是休息日
*/
private static boolean isWorkday(Date date, String[] workTimeArr) {
// 获取指定日期的星期
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
// 判断指定日期是否为周六或周日
if (dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.SUNDAY) {
return false;
}
// 判断指定日期是否为工作日
for (String workTime : workTimeArr) {
String[] timeArr = workTime.split("-");
String startTimeStr = timeArr[0];
String endTimeStr = timeArr[1];
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
try {
Date startTime = sdf.parse(startTimeStr);
Date endTime = sdf.parse(endTimeStr);
Calendar startCalendar = Calendar.getInstance();
startCalendar.setTime(startTime);
Calendar endCalendar = Calendar.getInstance();
endCalendar.setTime(endTime);
// 判断指定日期是否在工作时间段内
if (calendar.after(startCalendar) && calendar.before(endCalendar)) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
/**
* 计算指定日期的工作时长
*
* @param workTimeArr 工作时间段数组
* @param workStart 工作开始时间
* @param workEnd 工作结束时间
* @return 工作时长(分钟)
*/
private static int calculateWorkMinutes(String[] workTimeArr, Calendar workStart, Calendar workEnd) {
int workMinutes = 0;
for (String workTime : workTimeArr) {
String[] timeArr = workTime.split("-");
String startTimeStr = timeArr[0];
String endTimeStr = timeArr[1];
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
try {
Date startTime = sdf.parse(startTimeStr);
Date endTime = sdf.parse(endTimeStr);
Calendar startCalendar = Calendar.getInstance();
startCalendar.setTime(startTime);
startCalendar.set(Calendar.YEAR, workStart.get(Calendar.YEAR));
startCalendar.set(Calendar.MONTH, workStart.get(Calendar.MONTH));
startCalendar.set(Calendar.DAY_OF_MONTH, workStart.get(Calendar.DAY_OF_MONTH));
Calendar endCalendar = Calendar.getInstance();
endCalendar.setTime(endTime);
endCalendar.set(Calendar.YEAR, workEnd.get(Calendar.YEAR));
endCalendar.set(Calendar.MONTH, workEnd.get(Calendar.MONTH));
endCalendar.set(Calendar.DAY_OF_MONTH, workEnd.get(Calendar.DAY_OF_MONTH));
// 判断时间段是否与工作时间有重叠部分
if (workStart.before(endCalendar) && workEnd.after(startCalendar)) {
// 计算重叠部分的时间长度
int overlapMinutes = (int) ((Math.min(endCalendar.getTimeInMillis(), workEnd.getTimeInMillis())
- Math.max(startCalendar.getTimeInMillis(), workStart.getTimeInMillis())) / 1000 / 60);
// 累加工作时长
workMinutes += overlapMinutes;
}
} catch (Exception e) {
e.printStackTrace();
}
}
return workMinutes;
}
public static void main(String[] args) {
String workTimeStr = "09:00-12:00;13:30-18:00";
Date workStartTime = new Date(1625054400000L); // 2021-07-01 09:00:00
Date workEndTime = new Date(1625140800000L); // 2021-07-02 09:00:00
Date specifiedDate = new Date(1625126400000L); // 2021-07-02 03:00:00
int workMinutes = calculateWorkMinutes(workTimeStr, workStartTime, workEndTime, specifiedDate);
System.out.println("指定工作日工作的分钟数:" + workMinutes);
}
}
```
阅读全文