1、提交报工相关接口
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
package com.sdm.common.utils;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
|
||||
public class TimeCalculator {
|
||||
// 定义时间格式器
|
||||
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
/**
|
||||
* 计算两个时间的差值,返回以小时为单位的工时(四舍五入保留整数,Integer类型)
|
||||
*
|
||||
* @param beginTime 开始时间字符串 (yyyy-MM-dd HH:mm:ss)
|
||||
* @param finishTime 结束时间字符串 (yyyy-MM-dd HH:mm:ss)
|
||||
* @return 工时(小时),四舍五入保留整数,Integer包装类类型
|
||||
* @throws DateTimeParseException 时间格式解析失败时抛出
|
||||
*/
|
||||
public static Integer calculateWorkHours(String beginTime, String finishTime) {
|
||||
try {
|
||||
// 1. 将字符串转换为LocalDateTime对象
|
||||
LocalDateTime start = LocalDateTime.parse(beginTime, FORMATTER);
|
||||
LocalDateTime end = LocalDateTime.parse(finishTime, FORMATTER);
|
||||
|
||||
// 2. 计算时间差,获取总秒数
|
||||
Duration duration = Duration.between(start, end);
|
||||
long seconds = duration.getSeconds();
|
||||
|
||||
// 3. 转换为小时(秒数 / 3600),四舍五入保留整数
|
||||
double hours = seconds / 3600.0;
|
||||
// 四舍五入后转为Integer包装类(自动装箱)
|
||||
return Math.toIntExact(Math.round(hours));
|
||||
} catch (DateTimeParseException e) {
|
||||
System.err.println("时间格式解析失败,请确保时间格式为 yyyy-MM-dd HH:mm:ss");
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user