JAVA 计算指定工作日的工时 单位(分钟) 1.传入工作时段字符串,格式为String(HH:mm-HH:mm;HH:mm-HH:mm;HH:mm-HH:mm), 2.传入工作开始日期时间(Date类型) 3.传入工作结束日期时间(Date类型) 4.传入一个指定年月日(Date类型),该日期已知在工作开始和结束日期之间 5.根据工作的开始和结束日期 计算第4步中指定的这天一共工作了有多少(分钟)
时间: 2024-01-23 13:02:36 浏览: 36
以下是一个实现计算指定工作日工时的 Java 代码:
```java
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class WorkHoursCalculator {
public static void main(String[] args) throws ParseException {
String workHours = "09:00-12:00;13:00-18:00"; // 工作时段字符串
Date startDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2021-08-02 09:00:00"); // 工作开始日期时间
Date endDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2021-08-06 18:00:00"); // 工作结束日期时间
Date specifiedDate = new SimpleDateFormat("yyyy-MM-dd").parse("2021-08-04"); // 指定年月日
int workMinutes = calculateWorkMinutes(workHours, startDate, endDate, specifiedDate);
System.out.println("工作时长:" + workMinutes + " 分钟");
}
/**
* 计算指定工作日的工时(单位:分钟)
* @param workHours 工作时段字符串,格式为"HH:mm-HH:mm;HH:mm-HH:mm;..."
* @param startDate 工作开始日期时间
* @param endDate 工作结束日期时间
* @param specifiedDate 指定年月日
* @return 指定工作日的工时(单位:分钟)
*/
public static int calculateWorkMinutes(String workHours, Date startDate, Date endDate, Date specifiedDate) {
int workMinutes = 0;
Calendar calendar = Calendar.getInstance();
calendar.setTime(startDate); // 设置日历初始时间为工作开始日期时间
while (calendar.getTime().before(endDate)) { // 循环遍历工作时间段
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date currentDate = calendar.getTime();
if (sdf.format(currentDate).equals(sdf.format(specifiedDate))) { // 如果当前日期为指定日期
String[] workSegments = workHours.split(";"); // 分割工作时段字符串
for (String workSegment : workSegments) { // 遍历工作时段
String[] times = workSegment.split("-"); // 分割开始时间和结束时间
String startTime = times[0];
String endTime = times[1];
int startMinutes = getMinutes(startTime);
int endMinutes = getMinutes(endTime);
int workDayMinutes = endMinutes - startMinutes; // 当天的工作时间
int workMinutesInSegment = calculateWorkMinutesInSegment(currentDate, startTime, endTime); // 当天在该时间段内的工作时间
workMinutes += Math.min(workDayMinutes, workMinutesInSegment); // 取两者的最小值作为当天的工作时间
}
}
calendar.add(Calendar.DAY_OF_YEAR, 1); // 日历日期加1
}
return workMinutes;
}
/**
* 计算在指定时间段内的工作时间(单位:分钟)
* @param date 指定日期
* @param startTime 开始时间(格式为"HH:mm")
* @param endTime 结束时间(格式为"HH:mm")
* @return 在指定时间段内的工作时间(单位:分钟)
*/
private static int calculateWorkMinutesInSegment(Date date, String startTime, String endTime) {
int workMinutes = 0;
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
String currentDateStr = new SimpleDateFormat("yyyy-MM-dd").format(date);
String startDateTimeStr = currentDateStr + " " + startTime + ":00";
String endDateTimeStr = currentDateStr + " " + endTime + ":00";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date startDateTime = sdf.parse(startDateTimeStr);
Date endDateTime = sdf.parse(endDateTimeStr);
while (calendar.getTime().before(endDateTime)) {
if (!calendar.getTime().before(startDateTime) && !calendar.getTime().after(endDateTime)) {
int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
if (hourOfDay >= 9 && hourOfDay < 12 || hourOfDay >= 13 && hourOfDay < 18) { // 判断是否在工作时间段内
workMinutes++;
}
if (hourOfDay == 12) { // 中午休息1小时
calendar.add(Calendar.HOUR_OF_DAY, 1);
}
}
calendar.add(Calendar.MINUTE, 1);
}
} catch (ParseException e) {
e.printStackTrace();
}
return workMinutes;
}
/**
* 将时间字符串转换成分钟数
* @param time 时间字符串(格式为"HH:mm")
* @return 分钟数
*/
private static int getMinutes(String time) {
String[] times = time.split(":");
int hour = Integer.parseInt(times[0]);
int minute = Integer.parseInt(times[1]);
return hour * 60 + minute;
}
}
```
这个代码可以计算指定工作日的工时,工作时段字符串格式为"HH:mm-HH:mm;HH:mm-HH:mm;...",开始日期时间和结束日期时间为 Date 类型,指定日期也为 Date 类型。实现思路如下:
1. 遍历工作时间段,判断当前日期是否为指定日期。
2. 如果是指定日期,则遍历该工作时间段,计算当天在该时间段内的工作时间。
3. 计算当天的工作时间,取当天在该时间段内的工作时间和当天的工作时间的最小值作为当天的工作时间。
4. 遍历完所有工作时间段,累加每个指定工作日的工时,返回总工时。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)